github instagram email
Quickly Serve Static Files with Python
Aug 7, 2016
One minute read

When I’m doing local development on a static site, I use python to serve my files. Whether you use Python 2 or 3, there is simple command to serve files to any machine via http over the network including your localhost:

python3 -m http.server [port]
python2 -m SimpleHTTPServer [port]

Both commands default to port 8000.

Python3 made a lot of changes to the standard library. These differences in the 2vs3 commands reflects the module shuffle done in order to remove some of the more confusing conventions (or lack there of) found in Python2. CamelCase is typically reserved for classes in python and having modules like SimpleHTTPServer or StringIO never really made sense. Additionally, http.server behaves much better as a command line tool including help messages and better error handling that won’t just throw-up on you given an unknown argument.

~$ python3 -m http.server --help
usage: [-h] [--cgi] [--bind ADDRESS] [port]

positional arguments:
  port                  Specify alternate port [default: 8000]

optional arguments:
  -h, --help            show this help message and exit
  --cgi                 Run as CGI Server
  --bind ADDRESS, -b ADDRESS
                        Specify alternate bind address [default: all

Back to posts