Serving files
suggest changeThe standard library can serve files from a chosen directory:
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
host = "127.0.0.1"
port = 8000
handler = partial(SimpleHTTPRequestHandler, directory="public")
with ThreadingHTTPServer((host, port), handler) as server:
print(f"Serving at http://{host}:{port}")
server.serve_forever()
ThreadingHTTPServer handles each request in a separate thread. Passing directory explicitly avoids depending on the process's current working directory. Use this server only for development or a trusted local network; it is not a production web server.
For a command-line equivalent, change to the directory to serve and run:
python -m http.server 8000 --bind 127.0.0.1
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents