Last active
October 21, 2024 19:48
-
-
Save telmotrooper/84d8d4afbb294b599c6f443bbb36a456 to your computer and use it in GitHub Desktop.
Simple HTTPS Server in Python 3 (updated to Python 3.13)
This file contains hidden or 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
#!/usr/bin/env python3 | |
# To generate a certificate use: | |
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes | |
from http.server import HTTPServer, SimpleHTTPRequestHandler | |
import ssl | |
from pathlib import Path | |
port = 4443 | |
httpd = HTTPServer(("localhost", port), SimpleHTTPRequestHandler) | |
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | |
ssl_context.load_cert_chain(Path(__file__).parent / "server.pem") | |
httpd.socket = ssl_context.wrap_socket( | |
httpd.socket, | |
server_side=True, | |
) | |
print(f"Serving on https://localhost:{port}") | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, got it to working by following the certification generation using https://medium.freecodecamp.org/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec. Thanks for the python script.