【问题标题】:HTTPS to HTTP using CherryPy使用 CherryPy 的 HTTPS 到 HTTP
【发布时间】:2011-08-30 01:32:21
【问题描述】:

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()

【问题讨论】:

  • 为什么在 CherryPy 中这样做,而 Apache 可以更有效地做到这一点?

标签: python http https wsgi cherrypy


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

Andrew Cox 的链接再次断开,这是更新后的链接。我没有足够的分数来评论他的答案,因此有了新的答案。

https://cherrypy.readthedocs.org/en/3.3.0/refman/_cprequest.html#cherrypy._cprequest.Request.scheme

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2020-01-20
    • 2016-01-20
    • 1970-01-01
    • 2010-09-20
    • 2019-08-08
    • 2014-10-11
    相关资源
    最近更新 更多