使用CherryPy的HTTPS到HTTP

Mik*_*ike 4 python https wsgi http cherrypy

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()
Run Code Online (Sandbox Code Playgroud)

And*_*Cox 6

您可以检查request.scheme它是否为"https",然后您可以提出重定向.

请参阅http://docs.cherrypy.org/en/latest/refman/_cprequest.html?highlight=request.scheme#cherrypy._cprequest.Request.scheme