Sunday, December 28, 2014

Quickie web server.

If you need a fast setup web server running and you do not want to have to set up apache or something similar, then Python can help. Python comes with a simple builtin HTTP server. With the aid of this little HTTP server you can turn any directory (within limitations) in your system into your web server directory. This is important especially on embedded devices where storage is nominal. The only thing you need to have installed is Python. Linux comes with Python for the most part.

Practically speaking this is very useful to share files inside your local network. We like to use it for sharing documentation. Implementing this tiny but hugely useful HTTP server is very simple, its just a single line command. Note: this server is NOT SECURE. Use at your own risk.

Assume that I would like to share the directory /home/pwsrvr (or ~/pwsrvr) and my IP address is

Open up a terminal and type:
$ cd /home/your_dir_2_b_used
$ python -m SimpleHTTPServer 
c:\> cd /home/your_dir_2_b_used
c:\> python -m SimpleHTTPServer
That's it! Now your http server will start in port 8000. You will get the message:
$ python -m SimpleHTTPServer 8080
Serving HTTP on port 8080 ...

Now open a browser and type the following address: - - [28/Dec/2014 02:10:48] "GET / HTTP/1.1" 200 - - - [28/Dec/2014 02:10:48] "GET /ss.png HTTP/1.1" 200 - - - [28/Dec/2014 02:10:48] "GET /cgi.gif HTTP/1.1" 200 - - - [28/Dec/2014 02:10:48] "GET /ha.jpg HTTP/1.1" 200 -

You can also access it via:

If you want to use "localhost", make sure it is set up in your hosts file. Seen where it was not. If the directory has a file named index.html, that file will be served as the initial file. If there is no index.html, then the files in the directory will be listed.

$ ls
1.gif            cgi-bin    ha.jpg         ip.png  ss.png         wmfb.php
backup           cgi.gif    icon.ico       lo      thankyou1.php
bash1.png        chmod.png  index.html     sl.png  wmfb.dat
bashscript.html  cron.png   indexhtml.old  sp.png  wmfb.html

Index.html has been renamed to text. If you just want to serve files, this might be a better way to do it.

If you wish to change the port that's used start the program via:
$ python -m SimpleHTTPServer 8080

For more security, if you want to only serve on localhost you will need to write a custom Python program such as: (untested)
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 = 8000
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], "..."

Note also that this should also work on MSWindows or MSWindows with Cygwin. Have not tried on an Apple Mac. you can find Python at

No comments:

Post a Comment