Pages

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.

//Redhat/Centos/Fedora
# 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 0.0.0.0 .
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 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/SimpleHTTPServer.py", line 206, in ?
    test()
  File "/usr/lib64/python2.4/SimpleHTTPServer.py", line 202, in test
    BaseHTTPServer.test(HandlerClass, ServerClass)
  File "/usr/lib64/python2.4/BaseHTTPServer.py", line 569, in test
    httpd = ServerClass(server_address, HandlerClass)
  File "/usr/lib64/python2.4/SocketServer.py", line 330, in __init__
    self.server_bind()
  File "/usr/lib64/python2.4/BaseHTTPServer.py", line 101, in server_bind
    SocketServer.TCPServer.server_bind(self)
  File "/usr/lib64/python2.4/SocketServer.py", line 341, in server_bind
    self.socket.bind(self.server_address)
  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 0.0.0.0 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.
$cat webshare.py
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])
else:
    port = 1234
server_address = (’192.168.2.145', port)

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

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

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


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