Quick localized web server: python SimpleHTTPServer

This has been around for a while, but easy to forget if you need a basic web server for testing, etc., you can simply run:

python -m SimpleHTTPServer 8181

from the directory you want to be the web root, and you’re good to go. This is a built-in feature that will work on OS X and most *nix systems.

The great use case for this is when you have a folder with some files you want to test/view and there are AJAX or other features that need to run over HTTP but you don’t want to bother setting up a local test site or copying the files to existing one. Since it defaults to the folder you are in as the web root, you can also use root-relative links without a hassle. It defaults to port 8000, you can change that on the command line (as above).

Add a bash alias like alias ws="python -m SimpleHTTPServer" and then you can simply type ws from any directory to start a server.

If you need to change any other settings, see docs here

Leave a Reply