http.server is convenient for viewing static files, testing local requests, and building prototypes. The python -m http.server command serves the current directory.
Practical example
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
server = ThreadingHTTPServer(("127.0.0.1", 8000), SimpleHTTPRequestHandler)
print("Serving locally at http://127.0.0.1:8000")
try:
server.serve_forever()
except KeyboardInterrupt:
server.server_close()
Restrict the network interface
Use --bind 127.0.0.1 for local-only access and --directory public to choose the served content explicitly. Otherwise, files in the current directory may become visible to the network.
Keep customization small
SimpleHTTPRequestHandler resolves files and indexes. A subclass can adjust headers or logs for a test, but authentication and secure uploads call for a production-ready solution.
Shutdown and automation
In tests, choose an available port, run the server in a controlled context, and call shutdown() from another thread before server_close(). Do not leave orphan processes in CI.
Keep learning
Strengthen the foundation with safe subprocess execution. The official Python documentation, accessed July 22, 2026, documents the API, limitations, and version differences.