gpt4 book ai didi

python - 使用 CherryPy 的 HTTPS 到 HTTP

转载 作者:太空狗 更新时间:2023-10-30 02:34:38 24 4
gpt4 key购买 nike

CherryPy 是否可以将 HTTP 重定向到 HTTPS。例如,下面的代码是 http://example.com如果有人通过https://example.com访问我希望将它们重定向到纯 HTTP URL(可能是 301 重定向?)我该如何实现?

#!/usr/bin/env python

from pprint import pformat
from cherrypy import wsgiserver

def app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [pformat(environ)]

server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 80), app)

try:
server.start()
except KeyboardInterrupt:
server.stop()

最佳答案

您可以检查 request.scheme 如果它是“https”,那么您可以引发重定向。

参见 https://github.com/cherrypy/cherrypy/blob/f185ecd005d7fdbafb0ed83b0e49f05ac76e43fd/cherrypy/_cprequest.py#L218

关于python - 使用 CherryPy 的 HTTPS 到 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7237830/

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