In this article, we are going to learn how to set up a simple and local HTTP server using Python. An HTTP server can be very useful for testing Android, PC or Web apps locally during development. It can also be used to share files between two devices connected over the same LAN or WLAN network.
Installation
On the terminal run the following statement:
python3 -m http.server
or
python -m http.server
Accessing the server locally
For accessing the server locally you need to visit http://localhost:8000/. Here you can see all the directories of your local storage along with all the data. You can also access an HTML page, It will be rendered by your web browser as you access it.
Accessing the server over a Network
Before going into application make a note that the device on which you have run the above commands is called a Server or a Host and the second device that you will be using to access the Server over the network is called Client. For accessing the server over a Network make sure that both the device(Server and the Client) are connected over the same LAN or WLAN network. To access the Server you need the IP address of the server.
For obtaining the IP address the following steps are to be followed on your Server device:
On the Windows command prompt, execute the following statement:
ipconfig
On the Linux, Unix or macOS terminal, execute the following statement:
ifconfig
Note the IP address returned by the above command. We will use this IP address further.
Once you know the IP address open any web browser on the Client device and type in the IP address of the first machine(Server device), along with port 8000: http://[ip address]:8000
Congratulations!! Now you know how to host a simple HTTP server using Python.
Note: This HTTP server has limited security only so use it only for development purposes or sharing files locally it’s not recommended for use over a production environment.