Skip to main content

How to Share Files on a Local Network Using Python HTTP Server

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:

# Start the server on the default port 8000
python -m http.server

You 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 8080
Port Selection

If you see an OSError: [Errno 98] Address already in use error (or OSError: [Errno 48] on macOS), it means another process is already using that port. Choose any available port between 1024 and 65535. Common alternatives include 8080, 9000, and 3000.

Finding 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:

ipconfig

Look for the IPv4 Address entry under your active network adapter (Wi-Fi or Ethernet).

macOS:

ipconfig getifaddr en0

You can also find it in System Preferences > Network.

Linux:

hostname -I

Alternatively:

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:

  1. Open a web browser on your phone, tablet, or another computer.
  2. Navigate to http://YOUR_IP_ADDRESS:PORT.

For example:

http://192.168.1.105:8000

The browser will display a directory listing showing all files and folders. Clicking a file downloads it, and clicking a folder navigates into it.

Same Network Required

Both devices must be connected to the same local network, either the same Wi-Fi or the same wired network. A phone on cellular data or a device on a different network will not be able to reach the server without additional configuration such as port forwarding.

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:

# Only allow connections directed at this specific IP
python -m http.server 8000 --bind 192.168.1.105

# Only allow connections from the same machine (localhost)
python -m http.server 8000 --bind 127.0.0.1

Binding 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/folder

For example:

python -m http.server 8000 --directory ~/Documents/SharedFiles

This 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/assets

This 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.py

The try/except block around serve_forever() ensures the server shuts down cleanly when you press Ctrl+C instead of printing a traceback.

tip

You can extend the Handler class to add custom behavior such as logging requests, restricting access to certain file types, or adding custom response headers. However, for anything beyond basic file sharing, consider using a proper web framework like Flask or a dedicated file transfer tool.

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.

RiskDescription
No encryptionAll data is transferred in plain text over HTTP. Anyone on the network can intercept file contents.
No authenticationThere is no username or password. Anyone who knows your IP and port can browse and download files.
Full directory exposureEvery file and subfolder within the served directory is visible and downloadable.
No rate limitingThere is no protection against a client downloading large amounts of data or making excessive requests.
Never Use on Public Networks

Do not run this server on public Wi-Fi networks such as those in coffee shops, airports, or hotels. Anyone on the same network can discover your server and download every file you are sharing. Only use this tool on trusted private networks where you know and trust every connected device.

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 --bind to 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 9000

Cannot 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 ufw or iptables rules.
  • Confirm the correct IP address. Re-run the IP lookup command and make sure you are using the local IP, not 127.0.0.1 or 0.0.0.0.

Files or folders not appearing in the listing

  • Make sure you started the server in the correct directory, or verify the --directory path 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

TaskCommand
Start server (default port)python -m http.server
Custom portpython -m http.server 9000
Specific directorypython -m http.server --directory /path/to/folder
Bind to a specific IPpython -m http.server --bind 192.168.1.100
Combined optionspython -m http.server 9000 --bind 192.168.1.100 --directory /path
Stop the serverCtrl+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.server to start sharing files from the current directory immediately.
  • Use --directory to serve a specific folder and --bind to restrict which network interface the server listens on.
  • Find your local IP address with ipconfig (Windows), ipconfig getifaddr en0 (macOS), or hostname -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+C as 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.