Python – Swiss Army Knife Web Server

By Ed Taekema in Jython, Python on November 5th, 2008

I use the  following instant python test webserver in so many places… its quick to setup and makes sharing files a snap.  I first saw a mention of it by Gary Robbins.  Here it is:

python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Spurl
  • StumbleUpon
If you enjoyed this post, make sure you subscribe to my RSS feed!

About Ed Taekema

6 Comments

  1. I use a slight variation of the same tip. I did probably get it from the same source ages ago.

    start python -c “import os;print os.getcwd();import SimpleHTTPServer;SimpleHTTPServer.test()”

    I put this single line in a dirweb.cmd on Windows.

  2. There’s even simpler than that: just do python -m SimpleHTTPServer and you’re done. No need to fiddle with -c and SimpleHTTPServer.test()

  3. The “-m” option is indeed beautiful!

    Sadly, not the same luck in 3.0. I had hoped for:

    python3.0 -m http.server

    But it doesn’t run the file serving test first, so it has to be:

    python3.0 -c ‘import http.server as h; h.test(HandlerClass=h.SimpleHTTPRequestHandler)’

    Well, at least it works well.

  4. Thanks guys, I’ve updated my original post with the results of this discussion!

  5. @Gary Robinson -
    Excellent. This is such a handy technique.

  6. In python 3.1 “-m” option works again:
    $ python3.1 -m http.server
    Serving HTTP on 0.0.0.0 port 8000 …

    :D

Leave a Reply