相关疑难解决方法(0)

python flask从http重定向到https

我有一个使用python3.4和烧瓶的网站构建...我已经生成了自己的自签名证书,我目前正在通过localhost测试我的网站.

我正在使用python ssl模块以及这个烧瓶扩展:https://github.com/kennethreitz/flask-sslify

context = ('my-cert.pem', 'my-key.pem')
app = Flask(__name__)
sslify = SSLify(app)

...

if __name__ == '__main__':
    app.debug = False
    app.run(
    host="127.0.0.1",
    port=int("5000"),
    ssl_context=context
)
Run Code Online (Sandbox Code Playgroud)

然而,这似乎不起作用.我查看了sslify源代码,这行似乎没有用

def init_app(self, app):
    """Configures the configured Flask app to enforce SSL."""
    app.before_request(self.redirect_to_ssl)
    app.after_request(self.set_hsts_header)
Run Code Online (Sandbox Code Playgroud)

特别是函数调用redirect_to_ssl(我在redirect_to_ssl函数下添加了我自己的print语句,我的语句从未打印过)

def redirect_to_ssl(self):
    print("THIS IS WORKING")
    """Redirect incoming requests to HTTPS."""
    Should we redirect?
    criteria = [
        request.is_secure,
        current_app.debug,
        request.headers.get('X-Forwarded-Proto', 'http') == 'https'
    ]

    if not any(criteria) and not self.skip:
        if request.url.startswith('http://'):
            url = request.url.replace('http://', 'https://', 1) …
Run Code Online (Sandbox Code Playgroud)

python ssl flask

18
推荐指数
7
解决办法
3万
查看次数

标签 统计

flask ×1

python ×1

ssl ×1