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.
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
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.
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.
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.
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.
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.
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.
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.