Tech Tip: Really Simple HTTP Server with Python
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- RSS Feeds
- Readers' Choice Awards
- Tech Tip: Really Simple HTTP Server with Python
- DynDNS
2 hours 27 min ago - Reply to comment | Linux Journal
3 hours 21 sec ago - All the articles you talked
5 hours 23 min ago - All the articles you talked
5 hours 27 min ago - All the articles you talked
5 hours 28 min ago - myip
9 hours 53 min ago - Keeping track of IP address
11 hours 44 min ago - Roll your own dynamic dns
16 hours 57 min ago - Please correct the URL for Salt Stack's web site
20 hours 9 min ago - Android is Linux -- why no better inter-operation
22 hours 24 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
Simple HTTP Server in Java
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
Useful tip.
Regards,
Prevajanje Svitanok
Thanks!! nice tip! I'm
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
Thanks for asking, gives me a good reason to include this image:
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
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
Only works for me with sudo
Save Some Variable Assignments
Instead of
Why not
but i dont get the last script
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
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
You can find a nice Video Tutorial for hosting with Python as webserver at: http://digitizor.com/?p=1430
Hmmm...
Doesn't the Linux kernel come with an HTTP server built into it? No Python needed...
No
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
Thank you very much, this tip is absolutely amazing.
thanks alot
thanks great tip
tried it on windows machine with installed python
worked well
one line with netcat
really simple is this one using one line bash with netcat
while :; do nc -l 80 < my_nice.html ; done
Nice but Old
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
Wow.....that is indeed a useful tip....
I did not know that
Very useful! Thanks.