March 18, 2004
One line Python HTTP server
[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 -m SimpleHTTPServer
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 -m SimpleHTTPServer nnnn
python -m http.server nnnn
which lets you start a server on port nnnn for the current directory with:
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 at Nov 27, 2005 2:05:01 PM
Thanks for writing, I enjoyed your story!
Posted by: Gary Robinson at Nov 27, 2005 2:23:22 PM
That made my day. So simple!
Posted by: Arlo Emerson at Jun 10, 2008 6:45:28 PM
If it's too long, try
python -c 'from SimpleHTTPServer import test;test()'
Posted by: ZeD at Nov 6, 2008 1:51:37 AM
Even more compact:
python -m SimpleHTTPServer
Posted by: Niklas at Nov 6, 2008 11:39:17 AM
python3 -m http.server 8000
Posted by: duffy at Aug 11, 2010 3:45:20 AM
Thanks duffy, I'll update the post accordingly!
Posted by: Gary Robinson at Aug 11, 2010 11:34:03 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 at Oct 21, 2011 3:15:05 PM
That's pretty simple but very useful. Thanks for sharing this stuff.
Posted by: multiple ip hosting services at Dec 27, 2011 1:44:02 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 at Oct 13, 2012 8:53:37 PM