Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Access files of a devices in the same network using Python
When multiple devices are connected over the same network (LAN or WiFi), Python provides a simple way to share and access files between them. Python's built-in http.server module creates a lightweight web server that serves files from a chosen directory, making them accessible to other devices on the network.
The http.server module creates a simple HTTP server that serves files from the current directory when requests are made to the server. This article explains the step-by-step process to share files across network-connected devices using Python.
Step 1: Find the IP Address of the Device
To access files from a device, you need to know its IP address. Use the ipconfig command (Windows) or ifconfig (Linux/Mac) to find the IPv4 address of your device.
Finding IP Address
# On Windows ipconfig # On Linux/Mac ifconfig
Once you have the IP address, verify the device is reachable using the ping command ?
ping 192.168.1.7
The output shows the device is active and responding ?
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)
Step 2: Start the HTTP Server
Python's http.server module can be started directly from the command line. Navigate to the directory you want to share and run ?
python -m http.server 8000
This command starts the server on port 8000. You can use any available port number instead of 8000.
Starting Server Programmatically
You can also start the server from within a Python script ?
import http.server
import socketserver
import socket
# Get local IP address
hostname = socket.gethostname()
local_ip = socket.gethostbyname(hostname)
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Server running at http://{local_ip}:{PORT}/")
print("Press Ctrl+C to stop the server")
httpd.serve_forever()
When you run the server, Windows may show a firewall permission dialog. Click "Allow access" to enable network access.
Step 3: Access Device Files
Once the server is running, other devices on the same network can access the files by ?
Opening a web browser
Entering the device's IP address followed by the port number
For example, if the device IP is 192.168.1.7 and server runs on port 8000, access files at ?
http://192.168.1.7:8000/
This opens the file directory in a web browser, showing all files and folders. You can navigate through subdirectories by clicking on folder names.
Step 4: Stop the Server
To stop the server, press Ctrl+C in the command prompt or terminal where the server is running. This safely shuts down the HTTP server and stops file sharing.
Security Considerations
Be cautious when using http.server as it ?
Provides read-only access by default
Should only be used on trusted networks
Does not include authentication or encryption
Conclusion
Python's http.server module provides an easy way to share files across network-connected devices. Simply find your device's IP address, start the HTTP server on a chosen port, and access files through any web browser using the IP:port combination.
