[Most recently updated: Oct 29, 2013.]
One of the sweet things that comes with a Python installation is the ability to start up an http server in just one line. Since just about every modern computer (not counting tablets and phones!) has Python, you almost certainly can start up an http server to serve some files in a chosen directory with one quick line of code.
Go to your terminal. Go to the directory you want to serve HTML files from, then type one of the following, depending on your python version:
Python 2.x:
python -m SimpleHTTPServer
Python 3.x:
python -m http.server
Note that the output on your terminal will say:
Serving HTTP on 0.0.0.0 port 8000 ...
The mention of 0.0.0.0 does not mean the web server is listening at that IP address. Rather, it will be listening at 127.0.0.1:8000. The 0.0.0.0 signifies that the web server will respond to requests that end up on that machine no matter the hostname or IP that was requested.
Note that you can start your web server at port nnnn with:
Python 2.x:
python -m SimpleHTTPServer nnnn
Python 3.x:
python -m http.server nnnn
which lets you start a server on port nnnn for the current directory with:
http nnnn
This post has been updated a few times to include improved one-liners pointed out by readers as well as for clarity and completeness. I've deleted the original text since it's quite obsolete now. That might make some of the comments indecipherable because of the lack of context, but it's all for the greater good. If you have any comments/suggests/observations, please speak up!
This is great! Also see my story about how this is better than Samba, Apache and Firefox together ;-).
Posted by: Anton | November 27, 2005 at 02:05 PM
Thanks for writing, I enjoyed your story!
Posted by: Gary Robinson | November 27, 2005 at 02:23 PM
That made my day. So simple!
Posted by: Arlo Emerson | June 10, 2008 at 06:45 PM
If it's too long, try
python -c 'from SimpleHTTPServer import test;test()'
:)
Posted by: ZeD | November 06, 2008 at 01:51 AM
Even more compact:
python -m SimpleHTTPServer
Beautiful!
(Via masklinn's comment of Ed Taekema's blog post.)
Posted by: Niklas | November 06, 2008 at 11:39 AM
in python3:
python3 -m http.server 8000
Posted by: duffy | August 11, 2010 at 03:45 AM
Thanks duffy, I'll update the post accordingly!
Posted by: Gary Robinson | August 11, 2010 at 11:34 AM
Great tip. I have a Linux machine with customized OS and there is no repository to apt-get install from. So it would have been a pain to install Apache.
Saved me days of work.
Posted by: Gopal Shah | October 21, 2011 at 03:15 PM
That's pretty simple but very useful. Thanks for sharing this stuff.
Posted by: multiple ip hosting services | December 27, 2011 at 01:44 AM
This is a great tip! To make it even shorter I added this alias to ~/.bashrc
alias http="python -m SimpleHTTPServer"
Posted by: Paolo Dina | October 13, 2012 at 08:53 PM