22
Simple HTTP server in Python
Python
Web

Want to test your local static files quickly? Don't want the mess of Apache? Python can help you create a server in no time, all you need is Python installed on your system.

Python comes with a built-in module known as SimpleHTTPServer, which in other words is a simple HTTP server that gives you standard GET and HEAD request handlers. This module can turn any directory of your system into a web server. You just need to type a single line command in your terminal to implement this HTTP server.

For example if you wish to share a directory /home/python

Open up a terminal and type:

$cd /home/python
$python -m SimpleHTTPServer

Thats just it, fire up your browser and the present directory files can be seen on http://localhost:8000.

If the directory has a files name such as index.html, then that file will served as the initial file. Otherwise all the files present in the directory will be listed.

If port 8000 is already being by any other server, the above mentioned command accepts an optional port number as well.

$python -m SimpleHTTPServer <port_number>

This will start a server on the specified port.

For Python 3.x the command changes to:

$python3 -m http.server
Author

Notifications

?