File Sharing App using Python

Bluetooth and WhatsApp are often used to send files from one device to another. Although both methods are convenient, having a simple QR Code to access files from other devices is remarkably useful. With Python, you can create this functionality in minutes.

Using Python combined with networking concepts, we can build a simple file sharing application. This application provides both an IP address and QR Code scan the code or type the IP address to access shared files from any device on the same network.

Required Libraries

This application uses several Python modules ?

  • http.server Creates HTTP socket for web browser communication

  • socket and socketserver Manages network connections and creates servers

  • webbrowser Opens QR code in browser automatically

  • pyqrcode Generates QR codes from text/URLs

  • png Handles PNG image format for QR codes

  • os Interacts with operating system for file operations

We use TCP Port 8010 as it provides reliable communication between devices on the network.

Implementation Steps

Here's the complete file sharing application ?

# Import required modules
import http.server
import socket
import socketserver
import webbrowser
import pyqrcode
import png
import os

# Configuration
PORT = 8010

# Set the directory to share (user's home directory)
share_dir = os.path.join(os.path.expanduser('~'))

# Custom handler to serve files from specified directory
class FileShareHandler(http.server.SimpleHTTPRequestHandler):
    def translate_path(self, path):
        # Get absolute path of share directory
        root_dir = os.path.abspath(share_dir)
        
        # Normalize and split the requested path
        path = os.path.normpath(path)
        words = path.split('/')
        words = filter(None, words)
        
        # Build safe path within root directory
        path = root_dir
        for word in words:
            if os.path.dirname(word) or word in (os.curdir, os.pardir):
                continue
            path = os.path.join(path, word)
        return path

# Get system's IP address
def get_local_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(("8.8.8.8", 80))  # Connect to Google DNS
        ip = s.getsockname()[0]
    except Exception:
        ip = "127.0.0.1"
    finally:
        s.close()
    return ip

# Create server URL
local_ip = get_local_ip()
server_url = f"http://{local_ip}:{PORT}"

# Generate QR code
qr_code = pyqrcode.create(server_url)
qr_code.png("file_share_qr.png", scale=8)

# Open QR code in browser
webbrowser.open('file_share_qr.png')

# Start the server
print(f"File sharing server started on port {PORT}")
print(f"Access from browser: {server_url}")
print("Scan the QR code to access files from mobile devices")
print("Press Ctrl+C to stop the server")

# Create and start HTTP server
with socketserver.TCPServer(("", PORT), FileShareHandler) as server:
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("\nServer stopped")

The output shows the server details ?

File sharing server started on port 8010
Access from browser: http://192.168.1.178:8010
Scan the QR code to access files from mobile devices
Press Ctrl+C to stop the server

How It Works

The application follows these steps ?

  1. Directory Setup Sets the home directory as the sharing location

  2. Custom Handler Creates a handler to serve files securely from the specified directory

  3. IP Discovery Finds the local IP address by connecting to an external server

  4. QR Generation Converts the server URL into a scannable QR code

  5. Server Start Launches HTTP server and displays access information

Security Considerations

When using this application, keep these points in mind ?

  • Only devices on the same network can access shared files

  • The server shares your entire home directory be cautious about sensitive files

  • Stop the server when not needed to prevent unauthorized access

  • Consider creating a dedicated folder for sharing instead of the entire home directory

Troubleshooting

Common issues and solutions ?

  • Port already in use Change PORT to another number like 8011 or 9000

  • QR code not opening Manually navigate to the generated PNG file

  • Cannot access from mobile Ensure both devices are on the same WiFi network

  • Permission denied Run with appropriate permissions or choose a different directory

Conclusion

This Python-based file sharing application provides a quick way to access files across devices using QR codes. It's simpler than traditional methods and works entirely over your local network, ensuring privacy and speed for file access.

Updated on: 2026-03-27T13:18:22+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements