How to create Simple Python HTTP Server for 2.x and 3.x Version?
Python is really incredible with its simplicity and works on capability. In an earlier post, we have seen how to find the longest line in the file which is related to file operation. In this post, we see how simple one line command can turn your system into python HTTP server machine.
It does not require any external library. It is a just simple one command.
Command to create Python HTTP server:
Run the following command to create a python HTTP server on 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 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
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 process 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 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 server script.
You can access server files though 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:
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.
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 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 web browser. You can see the HTML index page output.
Once you have done with your use, you can stop the server by clicking “ctrl+c”. It interrupts the server and forces to stop.
We are continuing with an amazing non-exhaustive list for Python tricks and scripting. Do check this space for more updates. Free newsletter subscription is available only for enthusiastic program. Subscribe and don’t miss any of the tricks.