Blog post -

A simple https server for development

Last day, I had to serve some html pages via HTTPS. But didn't really know any simple solution to do this.

Here is one. A simple wrapper of the python SimpleHTTPServer with support for SSL.

Before running the server, you'll need to create your certificate:

openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes

Now that you have your certificate, create the file srv.py:

#!/usr/bin/python

## Run the following command to generate your certificate file
# openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes
import os
import BaseHTTPServer, SimpleHTTPServer
import ssl 
import sys 

cdir = os.getcwd()
os.chdir(cdir)

certFile = os.path.dirname(sys.argv[0]) + '/server.pem'

httpd = BaseHTTPServer.HTTPServer(('localhost', 4443), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.socket = ssl.wrap_socket (httpd.socket, certfile=certFile, server_side=True)
httpd.serve_forever()

Add this file to your path, or simply call it from wherever you want like that:

	/path/to/srv.py

This will serve the file in your current directory on https://localhost:4443

If you need a non https version to simply serve some static file, you can use the standard python simpleHTTPServer like this:

	python -m SimpleHTTPServer

Topics

  • Web services