gpt4 book ai didi

python - 在 Pyramid 中,如何从 http 重定向到 https?

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:21 24 4
gpt4 key购买 nike

如果您无权访问网络服务器,而您唯一的办法就是拦截请求,然后从 http 重定向到 https,最好的重定向方法是什么?

我尝试使用 NewRequest 查看订阅者。我做了这样的事情:

@subscriber(NewRequest)
def modify_protocol(event):
if re.search('some string', event.request.host_url):
event.request.scheme = 'https'

但是,这并没有达到我的预期。该页面仍在呈现。任何想法,将不胜感激。

提前致谢。

最佳答案

在一个 View 中:

if req.scheme == "http":
return HTTPFound("https://" + req.host + req.path_qs)

使用事件监听器:

@subscriber(NewRequest)
def redirect(event):
if event.request.scheme == "http":
raise HTTPFound("https://" + event.request.host + event.request.path_qs)

我研究了使用 sendget_response 的方法,但找不到太多。

关于python - 在 Pyramid 中,如何从 http 重定向到 https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com