Testing django over HTTPS with runserver

Django does not have a test server that supports HTTPS. But it is incredibly easy to create an HTTPS server that proxies to django test server, The tool to do this is stunnel.

To install on Arch Linux run pacman -S stunnel. I have not tested this on Ubuntu but apt-get install stunnel4 should do the trick.

Next you need to create a private key and certificate. The following command will do this in one step.

$ openssl req -x509 -newkey rsa:2048 -keyout /etc/stunnel/stunnel.key -out /etc/stunnel/stunnel.pem -days 365 -nodes

Now you need to create a settings file, it can be named anything you like. In this example I have used https.cfg. We will setup the https proxy to accept connections on port 8080 and forward them to the default django test server port 8000. The other settings set a few defaults, like running the proxy in the foreground and setting the ssl version.

$ vim https.cfg
pid =
cert=/etc/stunnel/stunnel.pem
key=/etc/stunnel/stunnel.key
sslVersion = SSLv3
foreground = yes

[https] 
accept=8080
connect=8000
TIMEOUTclose=1

Finally you will start the proxy and django test server then you can navigate to https://localhost:8080

$ stunnel https.cfg
$ HTTPS=on python manage.py runserver

That is all there is to it.

Categories: