Sunday, November 3, 2013

Simple HTTP Web Server using Python

Basic HTTP Web Server using Python

Python make use of SimpleHTTPServer — Simple HTTP request handler to start for starting a New Web Server to serve the file relative to current directory. We can setup this Simple Web Server within seconds without any effort.

Note :  Before moving ahead, I just want to let you know that this functionality is available only with Version 2.4 or Later. $”python -m http.server [port]” for Python 3.

Installation and Verification :

Install python, if you have don’t have installed, then please install.

# yum install python OR  #rpm –ivh python-version.rpm

//On Ubuntu
#sudo apt-get install python

//Verify the Installation(Redhat/Centos)
#yum list python                                                                                                   
Loaded plugins: downloadonly, rhnplugin, security
This system is receiving updates from RHN Classic or RHN Satellite.
Installed Packages
python.x86_64                                                      2.4.3-56.el5                                                      installed

Start Basic Web Server :

You can start basic web server by using one single command as below. By default it will get started on Port 8000 and IP .
$ python -m SimpleHTTPServer
Serving HTTP on port 8000 ...

Now you can access this using any IP on your system and Port 8000. You will be able to see all contents on current directory as below :

Now what if you have already another service using this default port i.e. 8000. In that case you’ll get exceptions :

$ python -m SimpleHTTPServer                                                                                              
Traceback (most recent call last):
  File "/usr/lib64/python2.4/", line 206, in ?
  File "/usr/lib64/python2.4/", line 202, in test
    BaseHTTPServer.test(HandlerClass, ServerClass)
  File "/usr/lib64/python2.4/", line 569, in test
    httpd = ServerClass(server_address, HandlerClass)
  File "/usr/lib64/python2.4/", line 330, in __init__
  File "/usr/lib64/python2.4/", line 101, in server_bind
  File "/usr/lib64/python2.4/", line 341, in server_bind
  File "", line 1, in bind
socket.error: (98, 'Address already in use')


Start Basic Web Server on different Port :

We can start Web Server other than default by passing Port as argument.
$ python -m SimpleHTTPServer 1234
Serving HTTP on port 1234 ...            
Above command starting Server on Port 1234.

Start Basic Web Server on different IP and Port :

                We can use below code snippet for starting Web Server on Any specified Port and IP.
import sys
import BaseHTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler

HandlerClass = SimpleHTTPRequestHandler
ServerClass  = BaseHTTPServer.HTTPServer
Protocol     = "HTTP/1.0"

if sys.argv[1:]:
    port = int(sys.argv[1])
    port = 1234
server_address = (’', port)

HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)

sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."

Run this python script:
$ python
Serving HTTP on port 1234 ...

Advantages :
Ø  comes with python (>= 2.4), no need to install anything.
Ø  no configuration needed.