Skip to content

Instantly share code, notes, and snippets.

@jamchamb
Forked from dergachev/simple-https-server.py
Last active February 18, 2021 01:49
Show Gist options
  • Save jamchamb/2998730bfb10e7b7160f4047e79461b7 to your computer and use it in GitHub Desktop.
Save jamchamb/2998730bfb10e7b7160f4047e79461b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Based on http://www.piware.de/2011/01/creating-an-https-server-in-python/
# generate server.pem with the following command:
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
# run as follows:
# python simple-https-server.py
# then in your browser, visit:
# https://localhost:4443
import argparse
import BaseHTTPServer, SimpleHTTPServer
import ssl
import os
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--host', type=str, default='localhost')
parser.add_argument('--port', type=int, default=4443)
parser.add_argument('--srvdir', type=str, default=None)
args = parser.parse_args()
base_dir = os.path.realpath(os.path.dirname(__file__))
cert_path = os.path.join(base_dir, 'server.pem')
if args.srvdir is None:
web_dir = os.path.join(base_dir, 'web')
else:
web_dir = args.srvdir
os.chdir(web_dir)
httpd = BaseHTTPServer.HTTPServer((args.host, args.port), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile=cert_path, server_side=True)
httpd.serve_forever()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment