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

时间:2019-03-26 21:23:06

标签: python python-2.7 flask wsgi cherrypy

我现在正在使用HTTP现在在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应用程序吗?如果没有,那么在Cheroot上针对HTTPS的Flask应用程序的简单示例是什么?

1 个答案:

答案 0 :(得分:2)

我想出了必要的修改。 带有https的Cheroot上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()