繁体   English   中英

尝试在 CherryPy 服务器上部署 Flask 应用程序

[英]Trying to deploy a Flask app on CherryPy server

我试图在 CherryPy 服务器上部署我的 Flask 应用程序。 我喜欢它的简单和简约的性质。

所以我 PIP'ed CherryPy 如下

pip install CherryPy-15.0.0-py2.py3-none-any.whl

并编写如下脚本-许多来源建议的非常普遍

from cherrypy import wsgiserver
from hello import app

d = wsgiserver.WSGIPathInfoDispatcher({'/': app})
server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 80), d)

if __name__ == '__main__':
   try:
      server.start()
   except KeyboardInterrupt:
      server.stop()

令我惊讶的是,我有导入错误。 经过几次谷歌搜索后,我了解到我必须将导入行更改为 cheroot 才能使其工作。

from cheroot.wsgi import Server
from cheroot.wsgi import PathInfoDispatcher

现在,我的代码运行良好。 但是,如果这是使用 CherryPy WSGI 服务器的正确方法,或者我是否使用了错误版本的 CherryPy,我有点困惑。 我很困惑,因为 Cheroot 似乎已经超过一岁了(可以追溯到 2014 年),但我在 CherryPy WSGI 服务器上找到的关于 Flask 的所有信息都使用from cherrypy import wsgiserver ,而不是from cheroot.wsgi import Server ,甚至最新的帖子。

这让我不确定我是否在做正确的事情。

有人可以解释一下这种混乱吗?

Cheroot ( src ) 是一个低级别的 HTTP 和 WSGI 服务器,它曾经是CherryPy ( src ) 的一部分,但不久前已被分解为一个单独的 repo。 所以以前的cherrypy.wsgiserver已经转移到cheroot.wsgi模块。

它是完全可替换的,旨在允许开发人员直接依赖 Cheroot,如果他们只使用 WSGI 服务器,而不需要 CherryPy 的其他部分。

因此,您可以通过以下方式以与版本无关的方式使用它:

try:
    from cheroot.wsgi import Server as WSGIServer, PathInfoDispatcher
except ImportError:
    from cherrypy.wsgiserver import CherryPyWSGIServer as WSGIServer, WSGIPathInfoDispatcher as PathInfoDispatcher

from hello import app

d = PathInfoDispatcher({'/': app})
server = WSGIServer(('0.0.0.0', 80), d)

if __name__ == '__main__':
   try:
      server.start()
   except KeyboardInterrupt:
      server.stop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM