How to create Simple Python HTTP Server for 2.x and 3.x Version?

How to create Simple Python HTTP Server for 2.x and 3.x Version?

Python is really incredible with its simplicity and capability.

In this tutorial, we see how simple one-line command can turn your system into a Python HTTP server machine.

Just only one-line command. That makes Python more interesting.

It does not require any external library. All the required modules come preinstalled with Python.

Install Python on your system and you are ready.

Command to Create Python HTTP Server

There are different commands for different Python versions.

Your system can have multiple Python versions installed on your system. You can always check the Python version installed on your system before starting the server.

Run the following command to create a Python HTTP server on the local machine.

python -m SimpleHTTPServer #default port 8080

Note: This command supports on Python 2.x version. It does not support for python 3.x.

If you run the same command on python 3.x, you can get an error as

python: No module named SimpleHTTPServer

Use the following command to create HTTP server on your workstation with Python 3.x version.

python -m http.server

Command to Run Simple Python HTTP Server

Port number can be anything and you can see it on command console after running this simple python script.

Note: If 8080 port is not used by any of the processes on your system, it will be the default port number.

Now your server is ready to access.

Accessing Python Server

To test it on, open browser. Type in the address bar as “localhost:8080” and run.

You can see all the directory files accessible through the browser. Other addresses through which you can access files is 127.0.0.1:8080.

You can also use your own computer private IP address. You can find your computer IP address using “ipconfig” command in command prompt.

When you access the python HTTP server through a browser, it sends the request and response in the form of HTML. You can see all the request and responses on python console where you run the server script.

 Console for Python HTTP Server Request Response
You can access server files through other devices as well. For that, it is necessary to connect the device to the same network on which python server is running.

Use cases for Python HTTP Server

As FTP Server:

How to create an FTP server using Python for file transfer?

This trick is very much helpful if you want to transfer data from computer to other devices.

Personally, I use it to transfer movies or some other big files from my computer to Smartphone.

Python FTP Server

You can see all the directory listing from the folder where we have created Python HTTP server.

I prefer to transfer the files this way. It does not require any USB cable, LAN cable or any storage device to transfer files. As it uses private IP addresses to connect server and other devices, it is necessary to have all devices connected to the same network.

As HTTP Server:

You can use the Python HTTP Server for web development purpose as well. You no need to install any third-party software. Just create Python HTTP Server and save all the webpages into the folder where you have created python server.

Suppose you have this HTML page.

<html>
<body>
<strong>Welcome to CSEStack</strong>


We live and breathe the code...
</body>
</html>

Save it in python server folder as “index.html”.

Now hit the server from a web browser. You can see the HTML index page output.

Python server as HTTP Server

Once you have done with your use, you can stop the server by clicking “ctrl+c”. It interrupts the server and forces to stop.

If you want to master Python, checkout complete Python tutorial. We are continuing with an amazing non-exhaustive list for Python tricks and scripting. Do check this space for more updates.

Happy Pythoning!

12 Comments

    1. Thank!

      If your mobile and computer are connected to the same network, you can open “localhost:8080” in your mobile browser.

      After that, you can browse through all the shared folders and contents in the folders.

      1. Thanks but I tried opening “localhost:8000” in my mobile browser and it didn’t work. Is it because I am connected to my hostel wifi on both my devices? And this might not work for a public wifi?

        1. If it is not working, try this. It works…

          Get the IP address of your system. You can get the IP address by running command

          ipconfig (on windows)
          ifconfig (on Unix)

          Open mobile browser and run URL

          ip_address:port_used_by_Python_for_http_server

Leave a Reply

Your email address will not be published. Required fields are marked *