Creating a Simple HTTP Server with one line of Python

Python can run a simple HTTP server, using a built-in module called SimpleHTTPServer this provides standard GET and HEAD request handlers.

The great thing about this is that you do not have to configure anything, you only have to have Python installed. This is perfect to use when you need a quick web server and don’t want to mess with setting up apache. You can use this to turn any directory on your system into a web server and comes in very handy if you need to get some files on to a remote server.

How To Setup Simple HTTP Server

Start by navigating to the directory that you want to share

$ cd /home/somedir

Then just type..

$ python -m SimpleHTTPServer

This will start the simple HTTP server serving you all the files and directories in your current working directory on a default port of 8000.

You can check this by typing http:// then the IP address of the server in any browser with a port of 8000 on the end.

You can also change the port to something else by adding a port number on the end of the command.

$ python -m SimpleHTTPServer 1337

Again this can be checked from any browser making sure you add the same port number you used in the command.

Python 3

All the previous commands where for Python 2, but don’t fear Python 3 still has a built-in module, it just uses http.server instead of SimpleHTTPServer like below.

$ python3 -m http.server 8000

Serve up a Web page

You don’t have to just show directories and files. if you add an index.html to the directory you’re sharing, the simple HTTP server will serve this page up instead.

Logging in the Terminal

As long as the HTTP server is running the terminal will update as data is loaded from the Simple HTTP 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 looking through any apache access log.