SSL in python3 with HTTPServer

Solution 1:

I found another (simpler) solution here: http://www.piware.de/2011/01/creating-an-https-server-in-python/

This is my working porting to python3:

from http.server import HTTPServer,SimpleHTTPRequestHandler
from socketserver import BaseServer
import ssl

httpd = HTTPServer(('localhost', 1443), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile='certificate.pem', server_side=True)
httpd.serve_forever()

Solution 2:

From Python 3.7 ssl.wrap_socket is deprecated, use SSLContext.wrap_socket instead:

check: https://docs.python.org/3.7/library/ssl.html#ssl.wrap_socket

from http.server import HTTPServer,SimpleHTTPRequestHandler
import ssl

httpd = HTTPServer(('localhost', 1443), SimpleHTTPRequestHandler)
sslctx = ssl.SSLContext()
sslctx.check_hostname = False # If set to True, only the hostname that matches the certificate will be accepted
sslctx.load_cert_chain(certfile='certificate.pem', keyfile="private.pem")
httpd.socket = sslctx.wrap_socket(httpd.socket, server_side=True)
httpd.serve_forever()