Last active
January 26, 2022 14:42
-
-
Save cbeuw/b7fb0a228f858f4bb2ce09a6dd560a36 to your computer and use it in GitHub Desktop.
Universal HTTP to HTTPS redirector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.server | |
class Redirector(http.server.BaseHTTPRequestHandler): | |
def do_GET(self): | |
host = self.headers["Host"] | |
secure_uri = f"https://{host}{self.path}" | |
self.send_response(301) | |
self.send_header("Location", secure_uri) | |
self.end_headers() | |
print(f"Redirected request {self.requestline} to {secure_uri}") | |
if __name__ == "__main__": | |
with http.server.ThreadingHTTPServer(("0.0.0.0", 80), Redirector) as httpd: | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment