A Simple HTTP Web Server in Python

Please share it on

If you are looking for a simple web server solution, then Python and PHP can help you. Python and PHP have built in HTTP web service feature. I have few html, text and picture files and want to serve it to my network temporarily. My requirement is very small so I choose this method.

Python-Logo

Here I’m using CentOS 6.5 Linux operating system and it is having Python version 2.6.6. In Python 2.6.6 and above version, you can use built in module called “SimpleHTTPServer” to get the HTTP services. In Python 3, the SimpleHTTPServer module has been merged into http.server

For example, In my home directory I’m having some pictures, to serve it via HTTP go to that directory and execute the below command.

# cd /home/sathish/Pictures/
# python -m SimpleHTTPServer 9000

You can access the HTTP service at http://127.0.0.1:9000 or http://your-local-ip:9000. In case your machine has multiple IP, then use netstat command to find out the 9000 port listening IP and the command is

netstat -plnatu | grep :9000

Below is the access log of  SimpleHTTPServer which displays after execution of the command.

\Serving HTTP on 0.0.0.0 port 9000 ...
localhost.localdomain - - [16/Dec/2014 20:36:53] "GET /IMG_6895.JPG HTTP/1.1" 200
localhost.localdomain - - [16/Dec/2014 20:36:57] "GET /IMG_6894.JPG HTTP/1.1" 200 
localhost.localdomain - - [16/Dec/2014 20:37:00] "GET /IMG_6893.JPG HTTP/1.1" 200 
localhost.localdomain - - [16/Dec/2014 20:37:02] "GET /IMG_6891.JPG HTTP/1.1" 200 
localhost.localdomain - - [16/Dec/2014 20:37:17] "GET /IMG_6890.JPG HTTP/1.1" 200 
localhost.localdomain - - [16/Dec/2014 20:37:20] "GET /IMG_6889.JPG HTTP/1.1" 200 
localhost.localdomain - - [16/Dec/2014 20:37:23] "GET /IMG_6881.JPG HTTP/1.1" 200 

Below is the simple web server python code,  you can copy it and create a simple python file to run web server. It will serve present working directory contents in your browser.

# vim webserver.py

#!/usr/bin/python
#For python > 2.6 < 3 versions, use "SimpleHTTPServer" import SimpleHTTPServer #For python > 3 version, use "http.server"
#import http.server
import SocketServer

PORT = 9000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

If you want to run this process in the background, you can use “screen” or “nohup” command.

For example :

I don’t have screen command package installed while writing this post, so I’m adding the installation step here

For RHEL 6.x/ CentOS 6.x, use this command

sudo yum install screen

For Ubuntu and Debian, use this command

sudo apt-get install screen

To run the process in background, start a screen

#screen -R webserver

-R option allows you to reattach the screen if exists, else it will create a new one.

webserver is the name of the screen

now execute this code

# python -m SimpleHTTPServer 9000

To get out from the screen, press “Ctrl + A + D”, It will display a message “[detached]”

Whenever you want to see the logs or kill the python script, you can reattach the screen with the help of same screen command.

If you want to run this on every system reboot, add the below lines in the /etc/rc.local

/usr/bin/python -m SimpleHTTPServer 9000 &

exit 0

or from the command line

echo "/usr/bin/python -m SimpleHTTPServer 9000 &" >> /etc/rc.local

echo "exit 0" >> /etc/rc.local

In the next post, I’m going to write about Simple Web Service using PHP.

If you have any other Simple HTTP web service answers, please let me know through comments.

Thanks!

Please share it on

Leave a Reply

Your email address will not be published. Required fields are marked *