Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
# generate server.xml with the following command:
# openssl req -new -x509 -keyout key.pem -out server.pem -days 365 -nodes
# run as follows:
# python simple-https-server.py
# then in your browser, visit:
# https://localhost:4443
import BaseHTTPServer, SimpleHTTPServer
import ssl
import distutils.spawn
import subprocess
import tempfile
import os
with tempfile.NamedTemporaryFile(suffix='.key',delete=False) as tkeyfile, tempfile.NamedTemporaryFile(suffix='.pem',delete=False) as tcertfile:
if distutils.spawn.find_executable('openssl'):
try:
c = subprocess.check_output(['openssl', 'req', '-new', '-x509', '-keyout', tkeyfile.name, '-out', tcertfile.name, '-days', '365', '-nodes', '-subj', '/CN=www.example.com/O=Example Inc./C=US'], stderr=subprocess.STDOUT)
fsha1 = subprocess.check_output(['openssl', 'x509', '-noout', '-fingerprint', '-sha1', '-inform', 'pem', '-in', tcertfile.name]).strip()
print(fsha1)
thumbsha1 = fsha1.split('=')[1].replace(":","").lower()
fsha2 = subprocess.check_output(['openssl', 'x509', '-noout', '-fingerprint', '-sha256', '-inform', 'pem', '-in', tcertfile.name]).strip()
print(fsha2)
thumbsha2 = fsha2.split('=')[1].replace(":","").lower()
httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, keyfile=tkeyfile.name, certfile=tcertfile.name, server_side=True)
print('https://localhost:4443/?'+thumbsha1+'&'+thumbsha2)
httpd.serve_forever()
finally:
tkeyfile.close()
tcertfile.close()
os.remove(tkeyfile.name)
os.remove(tcertfile.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.