Skip to content

Instantly share code, notes, and snippets.

@boris-arzur
Forked from dergachev/simple-https-server.py
Last active February 3, 2016 04:16
Show Gist options
  • Save boris-arzur/7c82795fd3c8550f451e to your computer and use it in GitHub Desktop.
Save boris-arzur/7c82795fd3c8550f451e to your computer and use it in GitHub Desktop.
#!py -2
# taken from http://www.piware.de/2011/01/creating-an-https-server-in-python/
# http://stackoverflow.com/questions/2455606/basichttpserver-simplehttpserver-and-concurrency
# http://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl
import SocketServer
import BaseHTTPServer
import SimpleHTTPServer
import ssl
import os
import random
cert_file = 'server.pem'
make_cert_cmd = """sh -c "openssl req -new -x509 -keyout %s -out %s -days 365 -nodes -sha256 -subj '/CN=$(hostname)'" """%(cert_file, cert_file)
if not os.path.isfile(cert_file):
if os.system(make_cert_cmd) != 0:
raise ValueError("no cert available, and failed to generate one")
# "https://raw.githubusercontent.com/python/cpython/2.7/Lib/test/ssl_servers.py"
class ForkingServer(SocketServer.ForkingMixIn, BaseHTTPServer.HTTPServer):
pass
port = 4443 + random.randint(1, 1000)
print "Listening on %d"%port
httpd = ForkingServer(('0.0.0.0', port), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile=cert_file, server_side=True)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment