简体   繁体   English

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

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

How can I redirect HTTP to HTTPS by default? 默认情况下,如何将HTTP重定向到HTTPS?

For example, if I type example_address.com I would like to get https://example_address.com 例如,如果我输入example_address.com,我想获取https://example_address.com

My code is below 我的代码如下

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

You could try running another server in port 80 and redirecting its requests to your https server. 您可以尝试在端口80中运行另一台服务器,并将其请求重定向到您的https服务器。

Something like this: 像这样:

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