Python: Setting up a webserver fast

In this post I want to show you how to setup a simple HTTP server on a Mac with Python. All you have to do is to open a terminal window and change to the root directory you want to serve with HTTP. In this example I will simply use the “Public” folder but you can choose whatever you need.

So let’s change the directory to “Public” first:

# cd Public

Next, we will start the webserver module by typing the following in the Terminal window:

# python -m SimpleHTTPServer

This will lookup the module SimpleHTTPServer and then run the corresponding py-file as a script. You have to make sure that you write the module name just like I did and don’t change the uppercase and lowercase letters; module names in Python are case-sensitive.

It takes a moment to start and as soon as it’s finished it will prompt with a short message. The webserver is started on port 8000 and binds not only to localhost by default but the placeholder IP address with four zeros which means that any host in the network can reach the webserver as long as there’s no firewall rule preventing this.

You can open a web browser and check out what this looks like. Simply go to localhost:8000. We can see all files – even the normally hidden dot files – and browse through the directories. Of course we can download files as well.

As long as the webserver is running it will print an access log to the console window. Just ignore any HTTP 404 warnings – most likely the browser tried to look for a favicon.ico (or similar) which just does not exist in this case. Finally, you can stop the webserver by simply hitting Ctrl+C in the Terminal window.