https / ssl basics (part 1 - BaseHTTPRequestHandler)

https / ssl basics (part 1 - BaseHTTPRequestHandler)
Photo by Emmanuel Edward / Unsplash

Long gone are the simple days where your http server could server unencrypted traffic, and pretty much everything requires a SSL connection now. For those like myself learning and trying to master this it can add a real layer of complexity.  But let us master the basics..

  • To make your localhost.pem file
openssl req -new -x509 -keyout localhost.pem -out localhost.pem -days 365 -nodes

Next we will fire up some boilerplate code for a basic server:

import http.server, ssl

from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer

server_address = ('localhost', 4443)
httpd = http.server.HTTPServer(server_address, http.server.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True, certfile='localhost.pem', ssl_version=ssl.PROTOCOL_TLS)

Because your have a non-trusted certificate not from a CA authority you will get a warning:

Accepting the risk to our own server:

From there you will have a basic http.server:

Linux Rocks Every Day