Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@ds-hwang ds-hwang commented Sep 28, 2018

👍

@unnamalai-kb

This comment has been minimized.

Copy link

@unnamalai-kb unnamalai-kb commented Jan 30, 2019

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

This comment has been minimized.

Copy link

@unnamalai-kb unnamalai-kb commented Jan 31, 2019

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

This comment has been minimized.

Copy link

@withmelody withmelody commented Apr 11, 2019

Thanks!

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