在openshift

时间:2016-10-13 16:18:18

标签: python python-2.7 openshift cherrypy

我在开放式装备上运行一个基于樱桃的应用程序。最近,每当我尝试访问该网站时,我都会收到“503服务暂时不可用”错误。检查日志,我看到我正在尝试导入CherryPy的ImportError。这很奇怪 - 在我的requirements.txt中,CherryPy被列为依赖项,并且以前用得很好。我仔细检查以确保我正在通往openshift activate_this.py的正确路径,这似乎是正确的。我不太确定下一步该去哪看;任何帮助,将不胜感激。谢谢!

失败的导入位于app.py的第14行:

import os
import files


virtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'], 'virtenv')
virtualenv = os.path.join(virtenv, 'bin', 'activate_this.py')
conf = os.path.join(files.get_root(), "conf", "server.conf")
try:
    execfile(virtualenv, dict(__file__=virtualenv))
    print virtualenv
except IOError:
    pass

import cherrypy
import wsgi


def mount():
    def CORS():
        cherrypy.response.headers["Access-Control-Allow-Origin"] = os.environ['OPENSHIFT_APP_DNS']

    cherrypy.config.update({"tools.staticdir.root": files.get_root()})
    cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS)
    cherrypy.tree.mount(wsgi.application(), "/", conf)


def start():
    cherrypy.engine.start()


def end():
    cherrypy.engine.exit()

if __name__ == "__main__":
    mount()
    start()

更新

我最终看到(当使用git bash CLI推送到openshift repo时)来自requirements.txt的依赖安装失败,但有一些例外,我还没有考虑过。然后继续尝试在setup.py中安装依赖项,这很好用。

关于使用中的端口问题......我不知道。我将我的启动从tree.mount和engine.start更改为quickstart,当我推送到openshift时一切正常。只是因为踢(因为我需要它来运行我的测试),我切换回cherrypy.tree.mount,推它,它工作得很好。

去图。

1 个答案:

答案 0 :(得分:1)

我使用了Openshift的app.py入口点。以下是有关如何使用Openshift上的金字塔框架启动服务器的几个示例。我使用女服务员作为服务器,但我也使用了cherrypy wsgi服务器。只需注释掉你不想要的代码。

<强> app.py

#Openshift entry point

import os

from pyramid.paster import get_app
from pyramid.paster import get_appsettings


if __name__ == '__main__':
    here = os.path.dirname(os.path.abspath(__file__))

    if 'OPENSHIFT_APP_NAME' in os.environ:                                          #are we on OPENSHIFT?
        ip = os.environ['OPENSHIFT_PYTHON_IP']
        port = int(os.environ['OPENSHIFT_PYTHON_PORT'])
        config = os.path.join(here, 'production.ini')
    else:
        ip = '0.0.0.0'                                                              #localhost
        port = 6543
        config = os.path.join(here, 'development.ini')

    app = get_app(config, 'main')                                                   #find 'main' method in __init__.py.  That is our wsgi app
    settings = get_appsettings(config, 'main')                                      #don't really need this but is an example on how to get settings from the '.ini' files

# Waitress (remember to include the waitress server in "install_requires" in the setup.py)
    from waitress import serve
    print("Starting Waitress.")
    serve(app, host=ip, port=port, threads=50)

# Cherrypy server (remember to include the cherrypy server in "install_requires" in the setup.py)
#     from cherrypy import wsgiserver
#     print("Starting Cherrypy Server on http://{0}:{1}".format(ip, port))
#     server = wsgiserver.CherryPyWSGIServer((ip, port), app, server_name='Server')
#     server.start()


#Simple Server
    # from wsgiref.simple_server import make_server
    # print("Starting Simple Server on http://{0}:{1}".format(ip, port))
    # server = make_server(ip, port, app)
    # server.serve_forever()

#Running 'production.ini' method manually.  I find this method the least compatible with Openshift since you can't
#easily start/stop/restart your app with the 'rhc' commands. Mabye somebody can suggest a better way :)

# #Don't forget to set the Host IP in 'production.ini'.  Use 8080 for the port for Openshift
# You will need to use the 'pre_build' action hook(pkill python) so it stops the existing running instance of the server on OS
# You also will have to set up another custom action hook so rhc app-restart, stop works.
# See Openshifts Origin User's Guide  ( I have not tried this yet)

#Method #1
    # print('Running pserve production.ini')
    # os.system("pserve production.ini &")

#Method #2
    #import subprocess
    #subprocess.Popen(['pserve', 'production.ini &'])