One-line HTTP server with Python

Most of the time, loading HTML files from the filesystem (with the file://localhost/Users/... sort of path in your URL bar) works fine - remote files are pulled in, Javascript can usually be run, and things are peachy.

Every once in awhile, though, you might run into a situation for which the file:// protocol is ill-suited — specifically, if/when you run into an error like this: Cross origin requests are only supported for HTTP.

In other words, I ran into this really cool snippet:

$ cd /home/somedir
$ python -m SimpleHTTPServer

... awhile ago, and needed it a couple of minutes ago, and could barely remember it (hat tip to LinuxJournal for being easily Googleable). So I'm posting it here for posterity, and to augment my clearly failing memory.

Running that command will make, by default, the index.html in /home/somedir accessible at http://localhost:8000.

Or you can pass in the preferred port (python -m SimpleHTTPServer 8080) into the command line instead.

Thumbs up. Onward!

  • Also useful: tying this sort of thing to something like localtunnel, to brainlessly / painlessly broadcast something on your local machine to the interwebs.

Let me know what you think on Twitter.