Access files of a devices in the same network using Python


If multiple devices are connected over the same network i.e LAN or wifi then Python provides a way to access the files of the devices sharing the same network. Python built-in http.server module allows us to easily access the files of the devices connected over the same network.

Http.server is a simple server that serves devices from the current directory or the chosen directory of the device when a request on the server is done. In this article, we will discuss the steps involved in accessing the files of a device in the same network using Python.

Step 1 : Find the IP Address of the Device

In order to access the files of a device you need to know the IP address of the device. You can get the IP address of the device using the ipconfig command which gives the IPv4 address of the device. Once you get the IP address you can use the ping command to see the status of the device as −

ping ip_address # Ip Address will be the device IP address.

The ping command checks the status of the IP address. If the IP address is active it will return a reply to the ping command. If the IP address of your device is 192.168.1.7 ping it by −

Example

ping 192.168.1.7

Output

Pinging 192.168.1.7 with 32 bytes of data:
Reply from 192.168.1.7: bytes=32 time<1ms TTL=128
Reply from 192.168.1.7: bytes=32 time<1ms TTL=128
Reply from 192.168.1.7: bytes=32 time<1ms TTL=128
Reply from 192.168.1.7: bytes=32 time<1ms TTL=128
Ping statistics for 192.168.1.7:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

Step 2 : Start HTTP Server

Python http.server module provides a simple HTTP server that serves files from the current directory. The server can be started using the following command on the command line −

Example

python -m http.server 8000

Http.server 8000 command starts the server on port 8000. Any available port can be used instead of 8000.

Output

On running the above command a permission window will appear on the screen asking for permission to allow access to the device. Click on “Allow access” to start the server. The server will get started on port 8000. You can use any valid port for the server.

Step 3 : Access Device Files

Once the server is running on the specified port (8000) you can access the files of the device by −

  • Open the web browser

  • Type the IP address of the device followed by the port on which server is running.(currently 8000)

If the IP address of the device is 192.168.1.7 and the HTTP server is running on port 8080 then we can access the files of the device by typing http://192.168.1.7:8000/ on the web browser which will open the current file directory of the device in an HTML page view. You can access any subdirectory by clicking on the directory name on the webpage.

Example

http://192.168.1.7:8000/

Output

You can hit the same URL on any device which is connected over the same network to access the files of the device on which the server is running.

Step 4 : Stop the Server

Once you have accessed the files of the device you can stop the server using the Ctrl+C Command on the command prompt.

Conclusion

In this article, we understood how we can access the files of a device from multiple devices connected over the same network using Python http.server module. Firstly, we found the IP address of the device using ipconfig command and then ran the HTTP server using the http.server module on the device. When the server is running we can type the IP address followed by the port number to access the files of the device. After accessing the files we can simply stop the server using the Ctrl+C command on the terminal or command prompt.

Updated on: 17-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements