How to Use Python ‘SimpleHTTPServer’ 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//).

# 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="https://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.

Reference Links

SimpleHTTPServer Docs

Thilina Uvindasiri
I'm a BSc Special (Hons) graduate in Information Technology and works as an Engineer-Systems. Love to work, explore and research Linux and play rugby as a hobby.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

21 thoughts on “How to Use Python ‘SimpleHTTPServer’ to Create Webserver or Serve Files Instantly”

    • @Igor,

      As far as you’ve static IP address, you can access SimpleHTTPServer from anywhere in the world using IP address and port..

      Reply
  1. Hi Thilina, thanks for the awesome post! I ran this as instructed (nohup python -m SimpleHTTPServer 80 &) however after 1-2 days, the server stops. Then I have to ps ax and kill SimpleHTTPServer manually and run again. Can you give me some advice- where to look at, why it stops after some time?

    Reply
    • Hi aerin,

      I think this can be happen due to a shell change. Cannot tell the exact reason without looking at it. But can you try using absolute path for the “nohup” command.
      Step 01. Enter “which nohup” in your logging shell.
      Below is the sample output.

      [root@tecminttest ~]# which nohup
      /usr/bin/nohup

      Step 02. Enter your command with absolute path for the “nohup” command.
      Eg:
      [root@tecminttest ~]# /usr/bin/nohup python -m SimpleHTTPServer 80 &

      Try this and let me know if the error still exists.

      Reply
  2. Hi,

    Thanks, but do you know how to keep it running in the background? Whenever I log out of the ssh session, it cannot server again and gives 404 error!!

    Reply
    • @Ahmad,
      Just add & string at the end of the command to keep the process running in background even after you logout from your SSH session. For example,

      # python –m SimpleHTTPServer &
      
      Reply
      • @Ravi/Ahmad,
        I don’t think that will work. If you ran the command by background and if you exit the terminal, it will get disconnected. Use below command instead. It will not disconnect the SimpleHTTPServer after you logout of the terminal.

        nohup python -m SimpleHTTPServer 9999 &

        Hope this will be useful

        Reply
  3. Hi, Thilina, there is a typo under point 4. : “(In my case, /x01/tecmint/)”. That should be “(In my case, /x01/)” as can be seen in the picture below. Best, Uwe

    Reply
  4. Nice article. I know python-3 has a simple inbuilt http server and I have seen it’s uses in django while testing. However, using this technique for serving files on an ad hoc basis in your LAN in your office or home is one of the easiest ways. When all you want is to send a few files to other machines in your LAN, you need not worry about the OS of the other machines or how to configure SAMBA, NFS, SSH etc cause python’s built in http sever makes it trivial. You can even share files like this to your mobiles phones as well.

    Reply

Leave a Reply to Parijatha Kumar Cancel reply

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.