在Flask Python中使用Http进行Https

Yas*_*eda 11 python https http flask

我有一个客户服务器应用程序.我设法使用这个使用SSl加密使它们通过https连接

    context = SSL.Context(SSL.SSLv3_METHOD)
    context.use_privatekey_file('/path_to_key/key.key')
    context.use_certificate_file('/path_to_cert/cert.crt')
    app.run(use_reloader=True, host='0.0.0.0',port=9020,ssl_context = context)
Run Code Online (Sandbox Code Playgroud)

现在我想使用http和https运行服务器.有没有可能的方法呢?

Tri*_*m21 11

第一件大事:不要在烧瓶中使用内置的Web服务器来进行任何繁重的工作.您应该使用真正的Web服务器,如apache(mod_wsgi)nginex + gunicore等.这些服务器有关于如何同时运行http和https的文档.


jdr*_*ahl 7

我建议尝试使用Flask-SSLify - https://github.com/kennethreitz/flask-sslify

用法

用法非常简单:

from flask import Flask
from flask_sslify import SSLify

app = Flask(__name__)
sslify = SSLify(app)
Run Code Online (Sandbox Code Playgroud)

如果您发出HTTP请求,它将自动重定向:

$ curl -I http://secure-samurai.herokuapp.com/
HTTP/1.1 302 FOUND
Content-length: 281
Content-Type: text/html; charset=utf-8
Date: Sun, 29 Apr 2012 21:39:36 GMT
Location: https://secure-samurai.herokuapp.com/
Server: gunicorn/0.14.2
Strict-Transport-Security: max-age=31536000
Connection: keep-alive
Run Code Online (Sandbox Code Playgroud)

安装

安装也很简单:

$ pip install Flask-SSLify
Run Code Online (Sandbox Code Playgroud)

  • 这不回答这个问题. (6认同)