#34 - CLI Monday: Python SimpleHTTPServer

Links, Code, and Transcript

In this episode, I will show you a quick and easy way of serving web content via Pythons SimpleHTTPServer and http.server modules. This can be really useful for quickly sharing files, or testing website content, via a browser to systems on your network.

We are going to cover how to do this in both Python 2 and 3 since the syntax is slightly different. Lets start with Python 2 example. Say we are sitting in a directory, and we want to serve the contents of that directory, maybe locally or over the network, so someone can view the directory via their browser. How would you do it? Well, we could install Apache, or Nginx, but a really simple and less permanent way is it use Python at the command line.

Let me go split screen here for a minute, with a browser in the top, and down in the bottom a terminal window. You can see I have a file called mooooo.html in the directory /home/jw/e34. Now I want to serve this directory out to people on the network so they can access it.

# python 2.x
python -m SimpleHTTPServer

Lets run python -m SimpleHTTPServer, and you will see it says we are serving this directory out, the here means that it is listening on all network interfaces, this might be a good or bad thing, just something to be aware of. In that if you share something, anyone who can access your machine, potentially has access to these files. Then over here we see that the server us listing on port 8000.

Lets jump over the the browser and test this out, by going to localhost:8000, and as you can see we are serving out the directory, as it corresponds to the directory down here. We can then click on this link and see the file contents. Then down in the terminal you can see that we are given logging data about who accesses the site and what they are looking at.

My personal use case for this quick and easy server is sharing downloadable media with computers on my home network. For example, I just download something and then want to share it with someone who cannot use scp or rsync but has access to a browser.

You can also remove the ability to list directory contents by renaming the file from mooooo.html to index.html. In addition to that, we can also change the port SimpleHTTPServer listens on by adding the port as an additional argument. Then lets start the server and test it out. Now you can see it is listening on port 1234. Heading back to the browser lets test this out. As you can see we no longer get a directory listing and the new port seems to work as expected.

# python 2.x
python -m SimpleHTTPServer 1234

In this example we focused on Python 2 since that it what is running on my desktop. However, some newer distributions are shipping Python version 3, so lets look at what the commands are for that. I should mention that I have included links to the documentation for both Python 2 and 3, along with the commands, just check out the episode notes below.

For Python 3, we just modify the command a little bit, using http.server, adding a port is the same via an additional argument, and as of Python version 3.4 you can now use the bind option, this allows you to bind the server to a specific IP address. Hopefully adding some additional security so that your server is not wide open out of the internet.

# python 3.x
python -m http.server
# python 3.x
python -m http.server 1234
# python 3.x
python -m http.server 1234 --bind

Okay, so hopefully next time you need to share a directory with someone over the network, Python might come to the rescue.

  • Published
  • Duration
    4 minutes
  • Download
    MP4 or WebM
You may also like...