Lots of people use web server for various tasks, but not all of them need a full-fledged web server which will take a lot of time to configure (if you are a naive) and will eat up system resources.

So, when you just need a webserver with GET and HEAD request handler, then you can use a module in python which will run a mini webserver on your system without eating up system resources and even won’t take much time to configure it.(At least you don’t need to run behind the system admins in your company to configure one for you)

This is a simple command which you will be using for the same.

# python -m SimpleHTTPServer

This will start a web server in your system by default on port 8000 and will serve the requests. You will see the output on your terminal about all the content this web server will be serving, something like this.

# python -m SimpleHTTPServer
Serving HTTP on port 8000 ...
practivate.adobe.com - - [21/Jan/2010 16:17:30] "GET / HTTP/1.1" 200 -
practivate.adobe.com - - [21/Jan/2010 16:17:32] "GET /honeypot.txt HTTP/1.1" 200 -
practivate.adobe.com - - [21/Jan/2010 16:17:36] "GET /iptables.txt HTTP/1.1" 200 -
practivate.adobe.com - - [21/Jan/2010 16:17:39] "GET /networking.txt HTTP/1.1" 200 -

You can test the same from your browser pointing it to http://localhost:8000.
The directory from where you will run this command will act as the DocumentRoot for the server.

If 8000 port is already used at your system or you want to use some other port for running this service then you can run the command like this:

# python -m SimpleHTTPServer 8080

This will run the server on port 8080. You can use any port over here.
If you want to stop the server then simply press CTRL+C.

This is something which is inbuilt in python, but if you want to impress someone with your scripting skills then you can use this small script.

Tip 1: Using the SimpleHTTPServer module

import SimpleHTTPServer
import SocketServer

# simple web server
# serves files relative to the current directory.

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer((“”, PORT), Handler)
print “serving at port”, PORT

Tip 2: Using the SimpleHTTPServer module as a proxy

The second trick which I will be showing over here will be using the SimpleHTTPServer module as a proxy. Over here also you don’t need to do big things. You can run this small script and things will run smoothly. In this, when sent to a proxy, the HTTP requests should include the full URI for the target server. This server uses urllib to fetch data from the target.

import SocketServer
import SimpleHTTPServer
import urllib

PORT = 1234

class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
self.copyfile(urllib.urlopen(self.path), self.wfile)

httpd = SocketServer.ForkingTCPServer((”, PORT), Proxy)
print “serving at port”, PORT

The server ignores drive letters and relative path names (such as ‘..’). However, it does not implement any other access control mechanisms, so be careful while using it.

Obviously you can write a much advanced script using this module only which could have way more features than this but this is the simplest one. Please feel free to comment if you find something better than this or this can be tweaked to give better results/performance.

Still after this if someone feel the need of full-fledged Apache server, then please refer the documentation “Install Apache on Linux“.


  1. http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer
  2. http://www.python.org/doc/1.5.2p2/lib/module-BaseHTTPServer.html
If you enjoyed this post, make sure you subscribe to my RSS feed!!!!

No related content found.