繁体   English   中英

如何将http重定向到https python http.server

[英]How to redirect http to https python http.server

默认情况下,如何将HTTP重定向到HTTPS?

例如,如果我输入example_address.com,我想获取https://example_address.com

我的代码如下

def run(server_class=HTTPServerV6, handler_class=Server, port=443):
    server_address = ('::', port)
    httpd = server_class(server_address, handler_class)
    httpd.socket = ssl.wrap_socket (httpd.socket, certfile='./secret_private_key.pem', server_side=True)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()

您可以尝试在端口80中运行另一台服务器,并将其请求重定向到您的https服务器。

像这样:

def runHTTP(server_class=HTTPServerV6, handler_class=Server, port=80):
server_address = ('::', port)
httpd = server_class(server_address, handler_class)
httpd.send_response(301)
httpd.send_header('Location','https://www.yourserver.com')
httpd.end_headers()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM