Friday, July 24, 2020

Quick and Dirty SSL HTTPD web server for testing. (Stupid python tricks)

So I found myself trying to run some webxr demos and couldn't because I didn't have an SSL server available.

I found a super simple trick with python for doing this with regular NON-SSL web pages.

python3 -m http.server 8000

This works on everything.  For windows I am using cygwin but any python environment should work.

This will create a webserver on port 8000 and server any files in the directory you ran it in and below.

But now I needed a Secure SSL version of this.

First Create your certificates, since this is a test server, there is no point in getting real ones.

I am going to use openssl to create self signed certs.

Most browsers will put up some scary warning that you can agree to the warnings and continue and have a secure session with using a self signed cert.

mkdir /home/johns/ssl/
cd /home/johns/ssl/

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

I found a number of examples python SSL code but most were outdated and had issues, I eventually got the code below to run well.

Super Simple SSL python.

from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl
bind_to_address = ''
server_port = 4443
ssl_key_file = "/home/johns/ssl/key.pem"
ssl_certificate_file = "/home/johns/ssl/cert.pem"
httpd = HTTPServer((bind_to_address, server_port), SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, server_side=True, keyfile=ssl_key_file, certfile=ssl_certificate_file)
print("Serving" % 4443)

to run just run: