gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:13:00 27 4
gpt4 key购买 nike

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

所以我像下面一样对 CherryPy 进行了 PIP

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 服务器的正确方法,或者我 pip 了错误版本的 CherryPy。我很困惑,因为 Cheroot 似乎已有一年多了(可以追溯到 2014 年),但我在 CherryPy WSGI 服务器上的 Flask 上找到的所有信息都使用 fromcherrypy import wsgiserver,而不是 from cheroot.wsgi import Server,甚至是最新的帖子。

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

有人可以解释一下这种困惑吗?

最佳答案

Cheroot ( src ) 是一个低级 HTTP 和 WSGI 服务器,它曾经是 CherryPy 的一部分( src ) 一次,但不久前已被分解到一个单独的存储库中。因此,之前的 cherrypy.wsgiserver 已移至 cheroot.wsgi 模块。

它是完全可替换的,旨在允许开发人员在仅使用 WSGI 服务器时直接依赖 Cheroot,而不需要 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()

关于python - 尝试在 CherryPy 服务器上部署 Flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50918276/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com