SimpleHTTPServer in Python 3

By | January 15, 2010

If you’ve been doing any testing of client code that uses urllib or httplib, you probably know about this command:

This starts a very simple server in the current working directory; it serves all files from that directory, and is, quite simply, the quickest way to get something set up if you want to test some kind of web parsing or client code. (It’s also handy if you want to fire up a server to easily share files from your hard drive for a few minutes).

SimpleHTTPServer has been merged with BaseHttpServer into the http.server package in Python 3. I couldn’t easily find documentation for the new command, and ended up writing the following simple code:

Then I did a bit more digging around and realized that this command does what the old one did.

My code performs a little differently (it only serves on the localhost interface), but if anyone is looking for the old SimpleHTTPServer command line, there you have it.

The http.server module is normally supposed to be as a base for creating more complicated server environments (see your favourite web framework, for example), but the fact that it can be executed directly has a great deal of utility as well.

By the way, if you didn’t know about SimpleHTTPServer, you might also be interested in the built-in smtpd server as well. I use this command frequently:

This runs a simple smtp server on the given interface and port, and outputs all mail sent to that port to the console. It is very useful for testing and debugging web-based send-mail forms and such. You can, of course, run a standard smtpd server by not passing the -c DebuggingServer.

2 thoughts on “SimpleHTTPServer in Python 3

  1. cactus

    “The http.server module is supposed to” ?
    Supposed to what?! I am on the edge of my seat.

    I hope it is “supposed to deliver tacos to your desk.”

    1. dusty

      I can’t imagine what unfinished sentence you’re speaking of O:-)

      It may not be a coincidence that I had poutine yesterday; the algorithm may be adapted to taco delivery.


Leave a Reply

Your email address will not be published. Required fields are marked *