After running into this in a StackOverflow question, I did a quick Google search to see
what others had come up with. The search brought up several results for Python 2
(naturally), but few in Python 3. The ones that were there, seemed unnecessarily
convoluted or complex.
So, onward to the Python standard library documentation we go. At first notice, the
various BaseHTTPRequestHandler and Co. are obviously not the way to go – they are
interested with the HTTP layer and not the socket.
We have to dig deeper.
The ssl.wrap_socket documentation seems to be exactly what we are looking for - it takes
a plain old socket and wraps it into an SSL context. It also allows you to specify
certificate files, ciphers, client- or server-side behaviour and so forth. Brilliant. We
only need to find a socket to wrap..
Aha! There is a socket we can use! Let’s see if we can. First, generate a certificate with openssl: