Skip to content

Instantly share code, notes, and snippets.

@telmotrooper
Last active October 21, 2024 19:48
Show Gist options
  • Save telmotrooper/84d8d4afbb294b599c6f443bbb36a456 to your computer and use it in GitHub Desktop.
Save telmotrooper/84d8d4afbb294b599c6f443bbb36a456 to your computer and use it in GitHub Desktop.
Simple HTTPS Server in Python 3 (updated to Python 3.13)
#!/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()
@unnamalai-kb
Copy link

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.

@withmelody
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment