Need to send a file from your PC to your phone, share a folder with a colleague sitting next to you, or quickly distribute files across devices on the same Wi-Fi? You don't need cloud services, USB drives, or third-party apps. Python includes a built-in HTTP server that turns any folder into a shareable file directory in seconds.
In this guide, you'll learn how to use Python's http.server module to share files over a local network, access them from any device, and understand the security implications.
The One-Line Command
Open a terminal (or Command Prompt) inside the folder you want to share and run:
python -m http.serverThat's it. Your files are now being served on port 8000 by default.
Output:
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...To use a different port (e.g., 9000):
python -m http.server 9000To bind to a specific network interface:
python -m http.server 8000 --bind 192.168.1.5How to Access the Shared Files
From the Same Computer
Open your browser and navigate to:
http://localhost:8000You'll see a directory listing of all files and folders in the directory where you started the server.
From Another Device on the Same Network
To access files from a phone, tablet, or another computer, you need your machine's local IP address.
Find your IP address:
| OS | Command | Look For |
|---|---|---|
| Windows | ipconfig |
IPv4 Address (e.g., 192.168.1.5) |
| macOS | ifconfig or ipconfig getifaddr en0 |
inet under en0 |
| Linux | ip a or hostname -I |
IP under your active interface |
Then, on the other device's browser, navigate to:
http://192.168.1.5:8000Replace 192.168.1.5 with your actual IP address. You'll see the same directory listing and can click any file to download it.
Displaying the Shareable URL Automatically
Typing IP addresses manually is tedious. Use this Python script to detect your local IP and print the ready-to-share URL:
import socket
def get_local_ip():
"""Get the local IP address of this machine."""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "127.0.0.1"
local_ip = get_local_ip()
port = 8000
print(f"Share this URL with devices on your network:")
print(f" http://{local_ip}:{port}")
print(f"\nOr access locally:")
print(f" http://localhost:{port}")Output:
Share this URL with devices on your network:
http://192.168.1.5:8000
Or access locally:
http://localhost:8000Sharing a Specific Directory
By default, the server shares the current working directory. To share a specific folder, use the --directory flag (Python 3.7+):
python -m http.server 8000 --directory /path/to/shared/folderFor example:
python -m http.server 8000 --directory ~/Documents/project-filesThis keeps your other files private while sharing only the intended directory.
Creating a Simple Upload Server
The built-in http.server only supports downloading files. If you also need to upload files from another device, create a custom server:
"""Simple HTTP server with file upload support."""
import http.server
import os
import cgi
class UploadHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
"""Handle file uploads."""
content_type = self.headers['Content-Type']
if 'multipart/form-data' not in content_type:
self.send_error(400, "Expected multipart/form-data")
return
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST'}
)
file_item = form['file']
if file_item.filename:
filepath = os.path.join(os.getcwd(), file_item.filename)
with open(filepath, 'wb') as f:
f.write(file_item.file.read())
self.send_response(200)
self.end_headers()
self.wfile.write(f"Uploaded: {file_item.filename}".encode())
print(f"Received: {file_item.filename}")
else:
self.send_error(400, "No file provided")
def do_GET(self):
"""Serve directory listing with an upload form."""
if self.path == '/':
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
files = os.listdir('.')
file_list = ''.join(f'<li><a href="{f}">{f}</a></li>' for f in sorted(files))
html = f"""
<html><body>
<h2>Upload a File</h2>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
<h2>Files</h2>
<ul>{file_list}</ul>
</body></html>
"""
self.wfile.write(html.encode())
else:
super().do_GET()
if __name__ == '__main__':
server = http.server.HTTPServer(('0.0.0.0', 8000), UploadHandler)
print("Server running at http://0.0.0.0:8000")
server.serve_forever()Security Considerations
Quick Reference
| Task | Command |
|---|---|
| Start server (default port 8000) | python -m http.server |
| Start on a specific port | python -m http.server 9000 |
| Share a specific directory | python -m http.server --directory /path/to/folder |
| Bind to a specific IP | python -m http.server --bind 192.168.1.5 |
| Stop the server | Ctrl+C |
| Access locally | http://localhost:8000 |
| Access from another device | http://<your-ip>:8000 |
Summary
Python's built-in http.server module lets you share files across devices on the same network with a single command.
- Run
python -m http.serverin any directory to instantly make its contents accessible via a web browser from any device on your Wi-Fi. - Use
--directoryto control which folder is shared, find your local IP withipconfigorip a, and always stop the server withCtrl+Cwhen you're done.
Remember that this server has no encryption or authentication, so use it only on trusted networks for non-sensitive files.