简体   繁体   English

Flask-Python重定向到https连接已重置

[英]Flask-Python redirection to https The connection was reset

I am a new Flask user and i have a problem. 我是一个新的Flask用户,我有一个问题。 I want to redirect all url from http to https but I am having this error: 我想将所有网址从http重定向到https,但我遇到此错误:

The connection was reset 连接被重置

This is my Flask code: 这是我的Flask代码:

#! /usr/bin/python
# -*- coding:utf-8 -*-

from flask import *
from OpenSSL import SSL
import psycopg2
import os
from datetime import timedelta
import sys
from flask_sslify import SSLify
reload(sys)  
sys.setdefaultencoding('utf8')
db_conn = psycopg2.connect("dbname=billjobs host=192.168.42.96 port=50434 user=username password=password")

app = Flask(__name__)
db = db_conn.cursor()
app.permanent_session_lifetime = timedelta(seconds=900)
sslify = SSLify(app)
app.secret_key='\xatYK\x1ba\x1dz\xa6-D\x9d\x97\x83\xfa\xcf\xcbd\xfa\xfb\x1a|\x08\x1af'
context = ('ssl.crt','ssl.key')
@app.route('/')
def pre_log():
    return render_template('index.html')
if __name__ == '__main__':  
    app.run(host="192.168.42.186", ssl_context=context, debug=False)

If I enter directly the address https ://192.168.42.186:5000 it work but with http only its not 如果我直接输入地址https://192.168.42.186:5000它可以工作,但只使用http而不是

Thanks for helping me in advance 谢谢你提前帮助我

You cannot do this using ssl_context and Werkzung (default server of Flask) now. 您现在无法使用ssl_contextWerkzung (Flask的默认服务器)执行此操作。 A functionality to allow this was proposed and rejected in 2014: auto http to https redirect ; 2014年提出并拒绝了允许此操作的功能: 自动http到https重定向 ; citing: 理由是:

That requires running another HTTP server. 这需要运行另一个HTTP服务器。 Werkzeug is not capable of that and IMO it's out of scope. Werkzeug不具备这一能力,IMO超出范围。 run_simple should only be used for development anyway. run_simple只应该用于开发。

So what's going on is your Flask application calls run_simple underneath, passing ssl_context and some other variables. 那么正在发生的事情是你的Flask应用程序调用下面的run_simple ,传递ssl_context和其他一些变量。 SSLify has no impact on your routing as long as you use ssl_context , because sole presence of this variable makes Werkzung host only using https schema. 只要您使用ssl_contextssl_context对您的路由没有影响,因为此变量的唯一存在使Werkzung主机仅使用https架构。 To get redirection from http to https, you need either to set up another server, listening at http and redirecting to https or migrate to other, more advanced server which allows redirection easily. 要从http重定向到https,您需要设置另一台服务器,监听http并重定向到https或迁移到其他更高级的服务器,以便轻松进行重定向。

I recommend to migrate to Apache or gunicorn. 我建议迁移到Apache或gunicorn。 Flask provides comprehensive instructions on deployment: Deployment Options . Flask提供有关部署的全面说明: 部署选项 Keep in mind that built-in server of Flask (Werkzung) is not suitable for production, as authors of Flask write: 请记住,Flask(Werkzung)的内置服务器不适合制作,因为Flask的作者写道:

While lightweight and easy to use, Flask's built-in server is not suitable for production as it doesn't scale well and by default serves only one request at a time. 虽然重量轻且易于使用,但Flask的内置服务器不适合生产,因为它不能很好地扩展,默认情况下一次只能提供一个请求。

Using Apache you could redirect all http requests using VirtualHost rule, listening at 80: 使用Apache,您可以使用VirtualHost规则重定向所有http请求,在80处监听:

<VirtualHost *:80>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   Redirect /secure https://mysite.example.com/secure
</VirtualHost>

See more on this on Redirect Request to SSL Apache wiki. 有关详细信息,请参阅重定向请求SSL Apache wiki。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM