How to start and use SimpleHTTPServer (Python)

Send Us a Sign! (Contact Us!)

Python’s SimpleHTTPServer is the classic quick solution for serving the files in a directory via HTTP (often, you’ll access them locally, via localhost). This is useful, because there are some things that don’t work with file: URLs in web browsers.

Why should I use it?

An advantage with the built-in HTTP server is that you don't have to install and configure anything. The only thing that you need, is to have Python installed.

That makes it perfect to use when you need a quick web server running and you don't want to mess with setting up apache.

You can use this to turn any directory in your system into your web server directory.

Using SimpleHTTPServer

SimpleHTTPServer is invoked like this (the parameter &port> is optional):

python -m SimpleHTTPServer <port>
(On MacOSX, Python is pre-installed and this command works out of the box.)

This will now show the files and directories which are in the current working directory. You can also change the port to something else:

$ python -m SimpleHTTPServer 8080

Let’s look at an example of using SimpleHTTPServer: During the following Unix shell interaction, we first list the files in the current directory and then start SimpleHTTPServer to serve it.

$ ls .
foo.html
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

Afterwards, we can access the following URLs:

  • http://localhost:8000/ lists the files in the current directory (namely, just foo.html). If there were a file index.html, it would be displayed, instead.
  • http://localhost:8000/foo.html displays the file foo.html in the current directory.
  • http://your_ip_address:8000
  • http://127.0.0.1:8000

As long as the HTTP server is running, the terminal will update as data are loaded from the Python web server.

You should see standard http logging information (GET and PUSH), 404 errors, IP addresses, dates, times, and all that you would expect from a standard http log as if you were tailing an apache access log file.

Customizing SimpleHTTPServer

The following Unix shell script demonstrates how to customize SimpleHTTPServer so that it serves files that have a given file name extension with a given media type. One case where that matters is Firefox being picky about the media type of the webapp.manifest.

#!/usr/bin/python

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
Handler.extensions_map.update({
    '.webapp': 'application/x-web-app-manifest+json',
});

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "Serving at port", PORT
httpd.serve_forever()

Summary

In this post we showed how you with minimal effort can setup a web server to serve content. It's a great way of serve the contents of the current directory from the command line.

While there are many web server software out there (Apache, nginx), using Python built-in HTTP server require no installation and configuration.