【问题标题】:How to run a Flask app on CherryPy WSGI server (Cheroot) using HTTPS?如何使用 HTTPS 在 CherryPy WSGI 服务器(Cheroot)上运行 Flask 应用程序?
【发布时间】:2019-08-17 09:10:22
【问题描述】:

我现在使用 HTTP 在 CherryPy Cheroot WSGI 服务器上运行 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 应用程序的简单示例是什么?

【问题讨论】:

    标签: python-2.7 flask wsgi cherrypy cheroot


    【解决方案1】:

    我想出了必要的修改。 使用 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()
    

    【讨论】:

    • AFAIR 你甚至可以省略 my_app = WSGIPathInfoDispatcher({'/': app}) 行并将 app 直接提供给 WSGI 服务器。
    • 哦,嘿,这里有一个关于 Python 2.7 EOL 的强制性提醒:pythonclock.org
    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 2020-01-10
    • 2017-04-11
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多