如何在CherryPy WSGI服务器(Cheroot)上使用HTTPS运行Flask应用程序?

3

我正在通过CherryPy Cheroot WSGI服务器的HTTP方式运行Python 2.7 Flask应用程序,如下所示。

from cheroot.wsgi import Server as WSGIServer
from cheroot.wsgi import PathInfoDispatcher as WSGIPathInfoDispatcher

from MyFlaskApp import app

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

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

我需要做些什么才能从此处迁移到HTTPS? 我找到了以下指示,但似乎不适用于我的应用程序。
from cheroot.server import HTTPServer
from cheroot.ssl.builtin import BuiltinSSLAdapter

HTTPServer.ssl_adapter = BuiltinSSLAdapter(
        certificate='cert/domain.crt', 
        private_key='cert/domain.key')

我可以将上述示例应用于我的Cheroot Flask应用程序吗?如果不行,你能提供一个简单的示例,用于Flask在Cheroot下开启HTTPS服务吗?

1个回答

5

我已经找出所需的修改方法。 关于Cheroot中使用https的Flask应用程序的信息不多,因此我想分享一下。

from cheroot.wsgi import Server as WSGIServer
from cheroot.wsgi import PathInfoDispatcher as WSGIPathInfoDispatcher
from cheroot.ssl.builtin import BuiltinSSLAdapter

from MyFlaskApp import app

my_app = WSGIPathInfoDispatcher({'/': app})
server = WSGIServer(('0.0.0.0', 443), my_app)

ssl_cert = "[path]/myapp.crt"
ssl_key = "[path]/myapp.key"
server.ssl_adapter =  BuiltinSSLAdapter(ssl_cert, ssl_key, None)

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

据我所知,您甚至可以省略 my_app = WSGIPathInfoDispatcher({'/': app}) 这一行,并直接将 app 提供给 WSGI 服务器。 - webknjaz -- Слава Україні
2
哦,顺带提醒一下Python 2.7的EOL:https://pythonclock.org/ - webknjaz -- Слава Україні

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接