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() |
I am trying this on windows 10 and Chrome browser v71.
On the chrome browser, I use "https://localhost:4443" as the URL.
and I get the following error:
Your connection is not private
NET::ERR_CERT_COMMON_NAME_INVALID
I have added the certifcate to Trusted certifcates in the browser, yet this error comes. I guess it has to do with the certificate generation and the "CN" field issued during certificate generation using the openssl
openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
Any clues?
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.
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍