Python 3.0+http.serverterm
http.server.HTTPServer
servers
A simple HTTP server built on socketserver.TCPServer: it listens on an address and creates a handler-class instance for every request.
Syntax
server = HTTPServer(server_address, RequestHandlerClass, bind_and_activate=True)
The server is single-threaded: while one request is being handled the others wait, so one slow client stalls everybody — use ThreadingHTTPServer for development. serve_forever() blocks the thread, and shutdown() must be called from another thread: calling it from a handler on the same thread hangs forever. The address ('', 8000) listens on every interface, exposing the server to the local network; use ('127.0.0.1', 8000) for yourself, and port 0 picks any free port.
Examples
from http.server import HTTPServer, BaseHTTPRequestHandler
# bind_and_activate=False: объект создан, порт не занят — удобно для показа
srv = HTTPServer(('127.0.0.1', 8000), BaseHTTPRequestHandler, bind_and_activate=False)
print(srv.server_address) # → ('127.0.0.1', 8000)
print(srv.RequestHandlerClass.__name__) # → BaseHTTPRequestHandler
srv.server_close()
# в настоящей программе: with HTTPServer(...) as srv: srv.serve_forever()
See also
Open in the full glossarydocs.python.orgReport an inaccuracy