Serving a folder of static files locally

By Eric Lathrop on

I often need to view a bunch of static HTML files in my browser locally, and I often need to open them with http:// rather than file:// URLs so related CSS or JavaScript files load correctly. Luckily, Python provides a simple web server, and most Linux distros include Python.

I wrote a slightly fancy shell script called static that starts this python web server in the current folder, and conveniently opens a tab in my browser pointing to that site.

Here's the code:

#!/bin/bash

set -meuxo pipefail

python -m http.server &
xdg-open http://localhost:8000/
fg