« Amazon hit by 'shopping cart' patents lawsuit | Main | On the subject of Hamas »

March 18, 2004

One line Python Web server

[Most recently updated: Aug 11, 2010.]

if you have python installed, 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 8000

Either one should start up a web server on port 8000 to serve your files.

[I haven't verified the Python 3 version; it was contributed by duffy in one of the comments. The previous version I had for Python 3 was "python3.0 -c ‘import http.server as h; h.test(HandlerClass=h.SimpleHTTPRequestHandler)’"]

This post has been updated a few times to include improved one-liners pointed out by readers. 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! 

March 18, 2004 in Python | Permalink

Comments

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

Beautiful!

(Via masklinn's comment of Ed Taekema's blog post.)

Posted by: Niklas at Nov 6, 2008 11:39:17 AM

in python3:

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

Post a comment