Quickly sharing files between devices on the same network is a common need. Whether you are transferring documents to a colleague, accessing files from your phone, or distributing resources during a workshop, setting up a dedicated file-sharing tool can feel like overkill for a one-off transfer.
Python includes a built-in HTTP server module that transforms any folder on your computer into a web-accessible file share with a single terminal command. No installation, no configuration files, and no third-party dependencies. This guide shows you how to use it effectively, how to customize its behavior, and how to stay safe while doing so.
Starting the Server
Open a terminal or command prompt, navigate to the folder you want to share, and run:
python -m http.serverYou will see output confirming the server is running:
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...At this point, every file and subfolder inside your current directory is accessible through a web browser.
Using a custom port
If port 8000 is already in use by another application, specify a different port number as an argument:
python -m http.server 8080Finding Your Local IP Address
Other devices on your network need your computer's local IP address to connect. The way to find it depends on your operating system.
Windows:
ipconfigLook for the IPv4 Address entry under your active network adapter (Wi-Fi or Ethernet).
macOS:
ipconfig getifaddr en0You can also find it in System Preferences > Network.
Linux:
hostname -IAlternatively:
ip addr show | grep "inet "Your local IP address will typically look like 192.168.x.x or 10.0.x.x.
Connecting from Other Devices
Once the server is running and you know your IP address, any device on the same network can access the shared files:
- Open a web browser on your phone, tablet, or another computer.
- Navigate to
http://YOUR_IP_ADDRESS:PORT.
For example:
http://192.168.1.105:8000The browser will display a directory listing showing all files and folders. Clicking a file downloads it, and clicking a folder navigates into it.
Binding to a Specific Network Interface
By default, the server listens on all network interfaces (0.0.0.0), meaning it accepts connections from any source. You can restrict access by binding to a specific IP address using the --bind flag:
python -m http.server 8000 --bind 192.168.1.105
python -m http.server 8000 --bind 127.0.0.1Binding to 127.0.0.1 is useful when you want to test locally without exposing the server to other devices on the network.
Serving a Different Directory
You do not need to cd into the folder you want to share. The --directory flag lets you specify any path directly:
python -m http.server 8000 --directory /path/to/folderFor example:
python -m http.server 8000 --directory ~/Documents/SharedFilesThis starts the server and serves the contents of SharedFiles regardless of your current working directory.
Combining Options
You can combine --bind, --directory, and a custom port in a single command:
python -m http.server 9000 --bind 192.168.1.105 --directory ~/Projects/assetsThis starts a server on port 9000, bound to 192.168.1.105, serving files from ~/Projects/assets.
Creating a Reusable Python Script
For repeated use or when you need additional control, you can wrap the server in a short Python script:
import http.server
import socketserver
import os
PORT = 8000
DIRECTORY = os.path.expanduser("~/Documents/SharedFiles")
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving '{DIRECTORY}' at http://0.0.0.0:{PORT}")
print("Press Ctrl+C to stop the server")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")Save this as file_server.py and run it with:
python file_server.pyThe try/except block around serve_forever() ensures the server shuts down cleanly when you press Ctrl+C instead of printing a traceback.
Security Considerations
Python's built-in HTTP server is designed for development and trusted local networks only. It lacks the security features required for any kind of public-facing or production deployment.
| Risk | Description |
|---|---|
| No encryption | All data is transferred in plain text over HTTP. Anyone on the network can intercept file contents. |
| No authentication | There is no username or password. Anyone who knows your IP and port can browse and download files. |
| Full directory exposure | Every file and subfolder within the served directory is visible and downloadable. |
| No rate limiting | There is no protection against a client downloading large amounts of data or making excessive requests. |
Reducing exposure
If you must use the server, take these precautions to minimize risk:
- Share only what is needed. Create a dedicated folder with only the files you intend to share, and serve that folder with
--directory. - Bind to a specific interface. Use
--bindto restrict which network interface accepts connections. - Stop the server immediately after the transfer is complete. Do not leave it running in the background.
- Check your firewall. Make sure your operating system firewall is active and configured to prompt you when applications listen on new ports.
Troubleshooting Common Issues
"Address already in use" error
Another process is already listening on the port you selected. Either stop that process or choose a different port:
python -m http.server 9000Cannot connect from other devices
- Verify both devices are on the same network. A phone on mobile data cannot reach a server on your home Wi-Fi.
- Check the host computer's firewall. On Windows, you may need to allow Python through Windows Defender Firewall. On Linux, check
ufworiptablesrules. - Confirm the correct IP address. Re-run the IP lookup command and make sure you are using the local IP, not
127.0.0.1or0.0.0.0.
Files or folders not appearing in the listing
- Make sure you started the server in the correct directory, or verify the
--directorypath is accurate. - Check the file permissions. The user running the Python process must have read access to the files.
Server stops when the terminal closes
The server runs as a foreground process tied to your terminal session. If you close the terminal window, the server stops. Keep the terminal open for as long as you need the server running.
Quick Reference
| Task | Command |
|---|---|
| Start server (default port) | python -m http.server |
| Custom port | python -m http.server 9000 |
| Specific directory | python -m http.server --directory /path/to/folder |
| Bind to a specific IP | python -m http.server --bind 192.168.1.100 |
| Combined options | python -m http.server 9000 --bind 192.168.1.100 --directory /path |
| Stop the server | Ctrl+C |
Summary
Python's built-in HTTP server provides an instant, zero-configuration solution for sharing files across devices on a local network. With a single command, you can turn any directory into a browsable, downloadable file share without installing additional software or setting up cloud storage.
Key points to remember:
- Use
python -m http.serverto start sharing files from the current directory immediately. - Use
--directoryto serve a specific folder and--bindto restrict which network interface the server listens on. - Find your local IP address with
ipconfig(Windows),ipconfig getifaddr en0(macOS), orhostname -I(Linux), and share it with the connecting device. - Never run this server on untrusted or public networks. It has no encryption, no authentication, and exposes all files in the served directory to anyone on the same network.
- Stop the server with
Ctrl+Cas soon as the file transfer is complete.
For anything beyond quick, one-off file transfers on a trusted network, consider dedicated tools or frameworks that offer encryption and access control.