Serve a local directory via HTTP/HTTPS with Python3 http.server

Originally posted on 2020-10-27

Need to serve a local directory from a web server?

python3 -m http.server

Start an HTTPS Python web server... first, generate a self-signed cert:

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365

Then start the web server. You'll likely need to type the password when the server starts.

#!/usr/bin/env python3

from http.server import HTTPServer, BaseHTTPRequestHandler
import ssl

httpd = HTTPServer(('', 4443), BaseHTTPRequestHandler)

httpd.socket = ssl.wrap_socket (httpd.socket,
        certfile='cert.pem', server_side=True)