Start a local HTTP file server with Python
Serve a directory for local testing with http.server, choose the bind address explicitly, and understand version-specific HTTPS support and exposure limits.
For local testing, run python3 -m http.server --bind 127.0.0.1 8000 and open http://127.0.0.1:8000. It serves files from the current directory. Select a directory containing only the files you intend to expose.
Choose the address, port, and directory
# Serve the current directory on this machine only.
python3 -m http.server --bind 127.0.0.1 8000
# Use a different port and an explicit directory.
python3 -m http.server --bind 127.0.0.1 --directory /srv/files 8080
The default port is 8000. --directory was added in Python 3.7. If you omit --bind, the server binds to all interfaces, not only localhost. To intentionally allow network access, bind to an appropriate interface address or 0.0.0.0 for all IPv4 interfaces, and check firewall and network exposure.
Start the server from Python
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
handler = partial(SimpleHTTPRequestHandler, directory="/srv/files")
with ThreadingHTTPServer(("127.0.0.1", 8000), handler) as server:
print("Serving at http://127.0.0.1:8000")
server.serve_forever()
ThreadingHTTPServer is available from Python 3.7 and handles requests in threads. HTTPServer is the non-threaded alternative. The command-line server also uses a threaded implementation in modern Python, so it is inaccurate to describe every http.server invocation as single-threaded.
Keep its scope limited
This module is intended for basic development and testing, not production hosting. It does not provide an application authentication system. Directory listings can reveal files when no index is present, and the simple handler follows symbolic links, which can expose files outside the selected directory. Binding to localhost limits network reachability but does not make an arbitrary directory safe to serve.
Use a suitable production server for persistent file hosting. For application APIs or uploads, use an application framework and a supported deployment server.
FAQ
Can it serve HTTPS? Python 3.14 added HTTPSServer, ThreadingHTTPSServer, and command-line TLS options such as --tls-cert and --tls-key. They require an appropriate certificate and key. Earlier versions need another TLS-serving approach, such as a dedicated development proxy. TLS support does not change this module's production limitations.
Can I run it in the background? A POSIX-shell command such as nohup python3 -m http.server --bind 127.0.0.1 8000 > http-server.log 2>&1 & can outlive the terminal, but it provides no restart or lifecycle management. Stop the process when the test ends.
Does it accept file uploads? SimpleHTTPRequestHandler implements file serving through GET and HEAD, not a general POST upload endpoint. Add a purpose-built application if you need uploads.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan