【问题标题】:Importing CherryPy fails on openshift在 openshift 上导入 CherryPy 失败
【发布时间】:2016-10-13 16:18:17
【问题描述】:

我在 openshift 齿轮上运行基于cherrypy 的应用程序。最近,每当我尝试访问该站点时,都会收到“503 服务暂时不可用”错误。检查日志,我看到我在尝试导入 CherryPy 时遇到 ImportError。这很奇怪——CherryPy 在我的 requirements.txt 中被列为依赖项,并且以前可以正常导入。我仔细检查以确保我获得了 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 存储库时)来自 requirements.txt 的依赖项安装失败,但有一些我还没有费心去研究的异常。然后它继续尝试在 setup.py 中安装依赖项,并且效果很好。

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

去看看。

【问题讨论】:

  • 我一直使用 setup.py 文件来定义我的依赖项。您是否进行了 pip freeze 以查看是否实际安装了cherrypy?
  • 根据developers.openshift.com/languages/python/… 的文档,requirements.txt 应该可以工作,而且过去肯定可以工作。也许这已经改变了?进入应用程序并执行 pip freeze 没有显示已安装cherrypy,也没有从 app.py 中打印帮助(“模块”)。因此,似乎 virtualenv 没有启动或者 pip install 失败或没有运行。不幸的是,对于为什么会发生这些事情,或者如何进行更多调试,我还没有猜到。
  • ssh 进入你的应用并输入'source python/virtenv/venv/bin/activate'。然后做一个点冻结。那么cherrypy安装了吗?如果没有,那肯定是没有安装。尝试使用 setup.py。
  • 啊,这就是 virtualenv 所在的地方。我做了一个 setup.py ,这似乎解决了依赖问题。奇怪,但谢谢!现在,cherrypy 正在尝试启动,但说我无法在端口 8080 上创建套接字(这是 OPENSHIFT_PYTHON_PORT 解析的)。在找到developers.openshift.com/managing-your-applications/… 后,我将启动应用程序的端口更改为 8000,但我仍然收到“无法创建套接字(127.0.0.1,8080)权限被拒绝”错误。有什么想法吗?
  • 见下面我的回答。我希望这会有所帮助。如果您还有其他问题,请告诉我。

标签: python python-2.7 openshift cherrypy


【解决方案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 &'])

【讨论】:

    猜你喜欢
    • 2017-08-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多