Skip to content

Instantly share code, notes, and snippets.

@cbeuw
Last active January 26, 2022 14:42
Show Gist options
  • Save cbeuw/b7fb0a228f858f4bb2ce09a6dd560a36 to your computer and use it in GitHub Desktop.
Save cbeuw/b7fb0a228f858f4bb2ce09a6dd560a36 to your computer and use it in GitHub Desktop.
Universal HTTP to HTTPS redirector
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