On-demand Web Server with Python

A friend of mine has just showed me something that is not only useful but extremely simple to use. I’d say it fits the mind-blowing category.

How many times have you wanted to serve a file on an HTTP server for someone to download it from? Well, just install Apache, you’d say. I used to do that, but it feels like overkill. Maybe Lighttpd? I honestly found it was easier to just host it somewhere else than install and configure any of these in case they’re not already in use.

But here’s another solution: use Python! It is installed by default on most Linux distributions, if not all. It is so widespread right now that, if you’re not using it already – be it for programming or as a dependency for something else you use – you will need it sooner than you think. And not only it should already be installed, it needs zero configuration!

Fire up your terminal and try this:

$ python -m SimpleHTTPServer 8000

Next, point your browser to http://localhost:8000. Voilà! You just got yourself a web server! In case you haven’t tried this yourself yet, a directory listing for the current directory should appear. Close it with Ctrl-C when you’re done with it.

Extra tip: you can create a directory under your home such as webjail, or whatever name suits you, and start your webserver from there. Create symbolic links for stuff you want avaiable so you can keep your other files “secret”. Unlike my previous Apache setups with public_html, symlinks do work, so there is no need for hard copies. The only difference from regular files is that they will show a “@” appended to the filename on the listing, but that’s not really an issue, is it? 😉


One Response to “On-demand Web Server with Python”

  1. Instant minimal web server « Tower of Air Says:

    […] enough to offer the current directory via HTTP on port 8080. This post describes an alternative which has the advantage of being available on almost every Linux […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: