Sl-Alex home lab

Simple web server with cgi-bin support using Python (no-code)

2021-08-11
Sl-Alex
Web

During the FW development for my WlanClock project I decided to replace the default web interface (which is in onion-os package) with my own. onion-os is run by uhttpd, an OpenWRT-specific http server. Unfortunately, uhttpd is not available in Ubuntu, which means I could not run my web interface locally.

There is one snap package, but it does not support cgi-bin folder for backend scripts, which is actively used by onion-os and which I was also hoping to use.

There is also a PPA package with uhttpd port for Ubuntu, but the support was limited to Ubuntu 18.04. In addition, I prefer to use PPA only if there is absolutely no other way to solve the problem. Fortunately, I found a better way to solve the problem and run everything on my laptop.

I bet you have Python installed. Does not matter which version, it will work with any. Just go to your web server root folder and run the following for python2:

python2 -m CGIHTTPServer

or for python3:

python3 -m http.server --cgi

It will serve all files from the root of the server as-is, files from cgi-bin folder will be processed as scripts.

Here is how a simple cgi-bin script might look like:

#!/bin/sh
echo "content-type: text/html; charset=UTF-8"
echo ""
echo "<html><head><title>Test page</title></head><body><h1>It works</h1><p>Sample output</p></body></html>"

Similar Posts

Next post: RPM meter