How to Use Python Simple HTTP Server to Create Webserver or Serve Files Instantly

SimpleHTTPServer is a python module which allows you to instantly create a web server or serve your files in a snap. Main advantage of python’s SimpleHTTPServer is you don’t need to install anything since you have python interpreter installed. You don’t have to worry about python interpreter because almost all Linux distributions, python interpreter come handy by default.
You also can use SimpleHTTPServer as a file sharing method. You just have to enable the module within the location of your shareable files are located. I will show you several demonstrations in this article by using various options.

Step 1: Check for Python Installation

1. Check whether python is installed in your server or not, by issuing below command.
# python –V 

OR

# python  --version
It will show you the version of the python interpreter you’ve got and it will give you an error message if it is not installed.
Check Python Version
Check Python Version
2. You’re lucky if it was there by default. Less work actually. If it was not installed by any chance, install it following below commands.
If you have a SUSE distribution, type yast in the terminal –> Go to Software Management –> Type ‘python’ without quotes –> select python interpreter –> press space key and select it –> and then install it.
Simple as that. For that, you need to have SUSE ISO mounted and configured it as a repo by YaST or you can simple install python from the web.
Install Python on Suse
Install Python on Suse
If you’re using different operating systems like RHEL, CentOS, Debian, Ubuntu or other Linux operating systems, you can just install python using yum or apt.
In my case I use SLES 11 SP3 OS and python interpreter comes installed by default in it. Most of the case you won’t have to worry about installing python interpreter on your server.

Step 2: Create a Test Directory and Enable SimpleHTTPServer

3. Create a test directory where you don’t mess with system files. In my case I have a partition called /x01 and I have created a directory called tecmint in there and also I have added some test files for testing.
Create Testing Directory
Create Testing Directory
4. Your prerequisites are ready now. All you have to do is try python’s SimpleHTTPServer module by issuing below command within your test directory (In my case, /x01/tecmint/).
# python –m SimpleHTTPServer
Enable SimpleHTTPServer
Enable SimpleHTTPServer
5. After enabling SimpleHTTPServer successfully, it will start serving files through port number 8000. You just have to open up a web browser and enter ip_address:port_number (in my case its 192.168.5.67:8000).
SimpleHTTPServer Directory Listing
Directory Listing
6. Now click on link 'tecmint' to browse files and directories of tecmint directory, see the screen below for reference.
Browse Directory Files
Browse Directory Files
7. SimpleHTTPServer serves your files successfully. You can see what has happened at the terminal, after you accessed your server through web browser by having a look at where you executed your command.
Python SimpleHTTPServer Status
Python SimpleHTTPServer Status

Step 3: Changing SimpleHTTPServer Port

8. By default python’s SimpleHTTPServer serves files and directories through port 8000, but you can define a different port number (Here I am using port 9999) as you desire with the python command as shown below.
# python –m SimpleHTTPServer 9999
Change SimpleHTTPServer Port
Change SimpleHTTPServer Port
Directory Listing on Different Port
Directory Listing on Different Port

Step 4: Serve Files from Different Location

9. Now as you tried it, you might like to serve your files in a specific location without actually going to the path.
As an example, if you are in your home directory and you want to server your files in /x01/tecmint/ directory without cd in to /x01/tecmint, Let’s see, how we will do this.
# pushd /x01/tecmint/; python –m SimpleHTTPServer 9999; popd;
Serve Files from Location
Serve Files from Location
Directory Listing on Different Port
Directory Listing on Different Port

Step 5: Serve HTML Files

10. If there’s a index.html file located in your serving location, python interpreter will automatically detect it and serve the html file instead of serving your files.
Let’s have a look at it. In my case I include a simple html script in the file named index.html and locate it in /x01/tecmint/.
<html>
<header><title>TECMINT</title></header>
<body text="blue"><H1>
Hi all. SimpleHTTPServer works fine.
</H1>
<p><a href="http://www.tecmint.com">Visit TECMINT</a></p>
</body>
</html>
Create Index File
Create Index File
Now save it and run SimpleHTTPServer on /x01/tecmint and go to the location from a web browser.
# pushd /x01/tecmint/; python –m SimpleHTTPServer 9999; popd;
Enable Index Page
Enable Index Page
Serving Index Page
Serving Index Page
Very simple and handy. You can serve your files or your own html code in a snap. Best thing is you won’t have to worry about installing anything at all. In a scenario like you want to share a file with someone, you don’t have to copy the file to a shared location or making your directories shareable.
Just run SimpleHTTPServer on it and it is done. There is a few things you have to keep in mind when using this python module. When it serves files it runs on the terminal and prints out what happens in there. When you’re accessing it from the browser or download a file from it, it shows IP address accessed it and file downloaded etc. Very handy isn’t it?
If you want to stop serving, you will have to stop the running module by pressing ctrl+c. So now you know how to use python’s SimpleHTTPServer module as a quick solution to serve your files. Commenting below for the suggestions and new findings would be a great favour to enhance future articles and learn new things.

No comments :