蟒蛇烧瓶重定向到http从http [英] python flask redirect to https from http

查看:159
本文介绍了蟒蛇烧瓶重定向到http从http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站建立使用python3.4和瓶...我已经产生了我自己的自签名证书,我目前正在通过本地测试我的网站。

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

  context =('my- cert.pem','my-key.pem')
app = Flask(__ name__)
sslify = SSLify(app)
$ b $ ...

if __name__ =='__main__':
app.debug = False
app.run($ b $ host =127.0.0.1,$ b $ port = int(5000 ),
ssl_context = context

这似乎不工作然而。我看了一下sslify的源代码,这行看起来并不起作用。

pre $ def $ init_app(self,app):
配置配置的Flask应用程序以强制执行SSL。
app.before_request(self.redirect_to_ssl)
app.after_request(self.set_hsts_header)


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



$ p $ def redirect_to_ssl(self):
print(THIS IS WORKING)
将传入的请求重定向到HTTPS 。
我们应该重定向吗?
criteria = [
request.is_secure,
current_app.debug,
request.headers.get('X-Forwarded-Proto','http')=='https'


如果没有任何(标准)而不是self.skip:
如果request.url.startswith('http://'):
url = request.url.replace('http://','https://',1)
code = 302
if self.permanent:
code = 301
r =重定向(url,code = code)
返回r

任何想法?

解决方案

对我来说,看起来你正在变得比它需要更复杂。这里是我在我的views.py脚本中使用的代码来强制用户进行HTTPS连接:

  @ app.before_request $ b $ ():
if request.url.startswith('http://'):
url = request.url.replace('http://','https://' ,1)
code = 301
return redirect(url,code = code)


I have a website build using python3.4 and flask...I have generated my own self-signed certificate and I am currently testing my website through localhost.

I am using the python ssl module along with this flask extension: 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
)

This does not seem to be working however. I took a look in the sslify source code and this line does not seem to be working

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)

Specifically the function call to redirect_to_ssl (I added my own print statement under the redirect_to_ssl function and my statement was never printed)

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)
            code = 302
            if self.permanent:
                code = 301
            r = redirect(url, code=code)
            return r

I am pretty new to python. Any ideas?

解决方案

To me, it appears you're making it more complicated than it needs to be. Here is the code I use in my views.py script to force user to HTTPS connections:

@app.before_request
def before_request():
    if request.url.startswith('http://'):
        url = request.url.replace('http://', 'https://', 1)
        code = 301
        return redirect(url, code=code)

这篇关于蟒蛇烧瓶重定向到http从http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆