Skip to content

Instantly share code, notes, and snippets.

@jamchamb
Forked from dergachev/simple-https-server.py
Last active February 18, 2021 01:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
#!/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