Skip to content

Instantly share code, notes, and snippets.

@telmotrooper
Created July 25, 2018 21:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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
#!/usr/bin/env python3
# Ported to Python 3 by Telmo "Trooper" (telmo.trooper@gmail.com)
#
# Original code from:
# http://www.piware.de/2011/01/creating-an-https-server-in-python/
# https://gist.github.com/dergachev/7028596
#
# 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
separator = "-" * 80
httpd = HTTPServer(("localhost", 4443), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile="./server.pem", server_side=True)
print(separator)
print("Server running on https://localhost:4443")
print(separator)
httpd.serve_forever()
@ds-hwang
Copy link

👍

@unnamalai-kb
Copy link

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?

@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