Have you ever wanted a really easy way to serve up some content from a directory? Me too. As usual: Python to the rescue. And here's the best part: you don't have to install, code, or configure anything. If you have Python installed, you're all set.

Start the Server

Using your terminal, navigate to the directory you want to serve:

 $ cd ~/my_fancy_website/

Then, run the following command:

If you have Python 2.7.x installed:

$ python -m SimpleHTTPServer

If you have Python 3.x installed:

$ python -m http.server

After a second or so, you'll see the following message:

Serving HTTP on 0.0.0.0 port 8000 ...

That was it, you're done. See? I told you it was easy. Now, read on if you'd like some more info about the server.

Tip: If you'd like to serve on a different port, just append the port number to the command. To use port 1337, for instance, the command would be:

Python 2.7.x

$ python -m SimpleHTTPServer 1337

Python 3.x

$ python -m http.server 1337

Notice that the IP address that the server is using is 0.0.0.0. I'll spare you the technical details and just tell you that this IP address means you're serving publicly, and (firewall settings permitting) anyone on your network can connect to your computer on port 8000 and see the content you're serving.

Connect to the Server

Now, point your browser at 127.0.0.1:8000.

When there is no index.html file present in the directory, this is what you get: a directory listing. If you'd like, you can supply an index.html file, and that will be displayed instead of the listing.

If you look back at your terminal window, you'll see a log of all the connections made to your server and the IP address of the computer that connected.

127.0.0.1 - - [09/Aug/2014 00:02:51] "GET / HTTP/1.1" 200 -

MIME

SimpleHTTPServer will auto-detect the correct MIME type of the file. For example, here is an HTML file displayed as a web page.

The End

There you have it: a one-liner to start a web server and serve the contents of a directory.