http.server — HTTP 伺服器¶
此模組定義了用於實作 HTTP 伺服器的類別。
警告
http.server is not recommended for production. It only implements
basic security checks.
可用性: not WASI.
此模組在 WebAssembly 平台上不起作用或無法使用。更多資訊請參閱 WebAssembly 平台。
One class, HTTPServer, is a socketserver.TCPServer subclass.
It creates and listens at the HTTP socket, dispatching the requests to a
handler. Code to create and run the server looks like this:
def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
- class http.server.HTTPServer(server_address, RequestHandlerClass)¶
This class builds on the
TCPServerclass by storing the server address as instance variables namedserver_nameandserver_port. The server is accessible by the handler, typically through the handler'sserverinstance variable.
- class http.server.ThreadingHTTPServer(server_address, RequestHandlerClass)¶
This class is identical to HTTPServer but uses threads to handle requests by using the
ThreadingMixIn. This is useful to handle web browsers pre-opening sockets, on whichHTTPServerwould wait indefinitely.在 3.7 版被加入.
- class http.server.HTTPSServer(server_address, RequestHandlerClass, bind_and_activate=True, *, certfile, keyfile=None, password=None, alpn_protocols=None)¶
Subclass of
HTTPServerwith a wrapped socket using thesslmodule. If thesslmodule is not available, instantiating aHTTPSServerobject fails with aRuntimeError.The certfile argument is the path to the SSL certificate chain file, and the keyfile is the path to file containing the private key.
A password can be specified for files protected and wrapped with PKCS#8, but beware that this could possibly expose hardcoded passwords in clear.
也參考
See
ssl.SSLContext.load_cert_chain()for additional information on the accepted values for certfile, keyfile and password.When specified, the alpn_protocols argument must be a sequence of strings specifying the "Application-Layer Protocol Negotiation" (ALPN) protocols supported by the server. ALPN allows the server and the client to negotiate the application protocol during the TLS handshake.
By default, it is set to
["http/1.1"], meaning the server supports HTTP/1.1.在 3.14 版被加入.
- class http.server.ThreadingHTTPSServer(server_address, RequestHandlerClass, bind_and_activate=True, *, certfile, keyfile=None, password=None, alpn_protocols=None)¶
This class is identical to
HTTPSServerbut uses threads to handle requests by inheriting fromThreadingMixIn. This is analogous toThreadingHTTPServeronly usingHTTPSServer.在 3.14 版被加入.
The HTTPServer, ThreadingHTTPServer, HTTPSServer and
ThreadingHTTPSServer must be given a RequestHandlerClass on
instantiation, of which this module provides three different variants:
- class http.server.BaseHTTPRequestHandler(request, client_address, server)¶
This class is used to handle the HTTP requests that arrive at the server. By itself, it cannot respond to any actual HTTP requests; it must be subclassed to handle each request method (for example,
'GET'or'POST').BaseHTTPRequestHandlerprovides a number of class and instance variables, and methods for use by subclasses.The handler will parse the request and the headers, then call a method specific to the request type. The method name is constructed from the request. For example, for the request method
SPAM, thedo_SPAM()method will be called with no arguments. All of the relevant information is stored in instance variables of the handler. Subclasses should not need to override or extend the__init__()method.BaseHTTPRequestHandler有以下實例變數:- client_address¶
Contains a tuple of the form
(host,