HTTPS to HTTP using CherryPy

…衆ロ難τιáo~ 提交于 2019-12-12 12:14:31

问题


Is it possible for CherryPy to redirect HTTP to HTTPS. Lets for example say the code below is http://example.com if someone visits via https://example.com I want them to be redirected to the plain HTTP URL (301 redirect maybe?) how do I accomplish this?

#!/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()

回答1:


You can check the request.scheme if it is "https" then you can raise a redirect.

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




回答2:


Andrew Cox's link is broken again, here's the updated link to it. I don't have enough points to comment on his answer, hence the new answer.

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



来源:https://stackoverflow.com/questions/7237830/https-to-http-using-cherrypy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!