Tech Tip: Really Simple HTTP Server with Python

 in

If you need a quick web server running and you don't want to mess with setting up apache or something similar, then Python can help. Python comes with a simple builtin HTTP server. With the help of this little HTTP server you can turn any directory in your system into your web server directory. The only thing you need to have installed is Python.

Practically speaking this is very useful to share files inside your local network. Implementing this tiny but hugely useful HTTP server is very simple, its just a single line command.

Assume that I would like to share the directory /home/hisam and my IP address is 192.168.1.2

Open up a terminal and type:

$ cd /home/somedir
$ python -m SimpleHTTPServer

That's it! Now your http server will start in port 8000. You will get the message:

Serving HTTP on 0.0.0.0 port 8000 ...

Now open a browser and type the following address:

http://192.168.1.2:8000

You can also access it via:

http://127.0.0.1:8000

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.

If you wish to change the port that's used start the program via:

$ python -m SimpleHTTPServer 8080

If you want to only serve on localhost you'll need to write a custom Python program such as:

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 = 8000
server_address = ('127.0.0.1', 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()

Note also that this should also work on Windows or Cygwin.

______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Simple HTTP Server in Java

Josh's picture

If you wanted something similar but in Java, there is a source code posting at planet-source-code.com for it called Simple Web Server.

Handy command

Prevajanje Svitanok's picture

Useful tip.

Regards,

Prevajanje Svitanok

Thanks!! nice tip! I'm

Anonymous's picture

Thanks!! nice tip!

I'm looking in the pydoc to see where the default port 8000 is specified but i can't find it.
How do you guys find these things out?

Use the Source Luke

Mitch Frazier's picture

Thanks for asking, gives me a good reason to include this image:

vader tux

Check line 566 of /usr/lib/python/BaseHTTPServer.py (or ../lib64/.. if you have a 64 bit O/S installed). If you have a different version of python installed the line number may be different.

Mitch Frazier is an Associate Editor for Linux Journal.

Where's the default port defined

Anonymous's picture

Thanks alot, found where they defined the port to 8000.
It was indeed in /usr/lib/python2.6/BaseHTTPServer.py on line 580 on my 64bit ubuntu machine.

Only works for me with sudo

Anonymous's picture

Only works for me with sudo

Save Some Variable Assignments

Lawrence D'Oliveiro's picture

Instead of

import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

HandlerClass = SimpleHTTPRequestHandler
ServerClass  = BaseHTTPServer.HTTPServer

Why not

from SimpleHTTPServer import SimpleHTTPRequestHandler as HandlerClass
from BaseHTTPServer import HTTPServer as ServerClass

but i dont get the last script

didnt know that either's picture

nice tip. But I just don't understand the script on the end. I suppose "to only serve on localhost" means limiting access to requests from localhost. So what if I want to limit requests to 123.145.167.189 or 192.168.1.* or similar? Any more detailed comment???

Localhost only

Mitch Frazier's picture

Yes it means only listen on the localhost, not on any external interfaces.

To change the address that is used, change the line 'server_address' assignment:

  server_address = ('127.0.0.1', port)

for example:

  server_address = ('123.45.67.89', port)

However, wildcards in the IP address are not supported so there's no way to listen just on a particular subnet (eg. 192.168.1.*).

Mitch Frazier is an Associate Editor for Linux Journal.

You can find a nice Video

Mankad's picture

You can find a nice Video Tutorial for hosting with Python as webserver at: http://digitizor.com/?p=1430

Hmmm...

Ed Carp's picture

Doesn't the Linux kernel come with an HTTP server built into it? No Python needed...

No

Mitch Frazier's picture

Most Linux distributions come with HTTP servers, apache being the most common, but there's not one in the kernel.

Mitch Frazier is an Associate Editor for Linux Journal.

Thank you very much

kernel_script's picture

Thank you very much, this tip is absolutely amazing.

thanks alot

MohamadAtef's picture

thanks great tip
tried it on windows machine with installed python
worked well

one line with netcat

Anonymous's picture

really simple is this one using one line bash with netcat

while :; do nc -l 80 < my_nice.html ; done

Nice but Old

Anonymous's picture

Commandlinefu does have so many such great tip:
http://www.commandlinefu.com/commands/browse/sort-by-votes

But yes its really really useful

--tej

Wow.....that is indeed a

manseel's picture

Wow.....that is indeed a useful tip....

I did not know that

smpratz's picture

Very useful! Thanks.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState