-
-
Save boris-arzur/7c82795fd3c8550f451e to your computer and use it in GitHub Desktop.
This file contains 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
#!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