Publication date: 2020-07-03
Use "ssl.wrap_socket" to add ssl support. With just a single line, this makes the http server an https server:
import http.server
import ssl
httpd = http.server.HTTPServer(('localhost', 443), http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile='key.pem', certfile='server.pem', server_side=True)
httpd.serve_forever()
The certificate files need to be present. A self-signed certificate for testing can be created using openssl:
openssl req -new -x509 -keyout key.pem -out server.pem -days 365 -nodes
https://piware.de/2011/01/creating-an-https-server-in-python/ https://gist.github.com/dergachev/7028596