http.client --- HTTP 協定用戶端¶
This module defines classes that implement the client side of the HTTP and
HTTPS protocols. It is normally not used directly --- the module
urllib.request uses it to handle URLs that use HTTP and HTTPS.
也參考
The Requests package is recommended for a higher-level HTTP client interface.
備註
HTTPS support is only available if Python was compiled with SSL support
(through the ssl module).
可用性: not WASI.
此模組在 WebAssembly 平台上不起作用或無法使用。更多資訊請參閱 WebAssembly 平台。
The module provides the following classes:
- class http.client.HTTPConnection(host, port=None, [timeout, ]source_address=None, blocksize=8192)¶
An
HTTPConnectioninstance represents one transaction with an HTTP server. It should be instantiated by passing it a host and optional port number. If no port number is passed, the port is extracted from the host string if it has the formhost:port, else the default HTTP port (80) is used. If the optional timeout parameter is given, blocking operations (like connection attempts) will timeout after that many seconds (if it is not given, the global default timeout setting is used). The optional source_address parameter may be a tuple of a (host, port) to use as the source address the HTTP connection is made from. The optional blocksize parameter sets the buffer size in bytes for sending a file-like message body.For example, the following calls all create instances that connect to the server at the same host and port:
>>> h1 = http.client.HTTPConnection('www.python.org') >>> h2 = http.client.HTTPConnection('www.python.org:80') >>> h3 = http.client.HTTPConnection('www.python.org', 80) >>> h4 = http.client.HTTPConnection('www.python.org', 80, timeout=10)
在 3.2 版的變更: 新增 source_address。
在 3.4 版的變更: The strict parameter was removed. HTTP 0.9-style "Simple Responses" are no longer supported.
在 3.7 版的變更: 新增 blocksize 參數。
- class http.client.HTTPSConnection(host, port=None, *, [timeout, ]source_address=None, context=None, blocksize=8192)¶
A subclass of
HTTPConnectionthat uses SSL for communication with secure servers. Default port is443. If context is specified, it must be assl.SSLContextinstance describing the various SSL options.Please read Security considerations for more information on best practices.
在 3.2 版的變更: 新增 source_address、context 與 check_hostname。
在 3.2 版的變更: This class now supports HTTPS virtual hosts if possible (that is, if
ssl.HAS_SNIis true).在 3.4 版的變更: The strict parameter was removed. HTTP 0.9-style "Simple Responses" are no longer supported.
在 3.4.3 版的變更: This class now performs all the necessary certificate and hostname checks by default. To revert to the previous, unverified, behavior
ssl._create_unverified_context()can be passed to the context parameter.在 3.8 版的變更: This class now enables TLS 1.3
ssl.SSLContext.post_handshake_authfor the default context or when cert_file is passed with a custom context.在 3.10 版的變更: This class now sends an ALPN extension with protocol indicator
http/1.1when no context is given. Custom context should set ALPN protocols withset_alpn_protocols().在 3.12 版的變更: The deprecated key_file, cert_file and check_hostname parameters have been removed.
- class http.client.HTTPResponse(sock, debuglevel=0, method=None, url=None)¶
Class whose instances are returned upon successful connection. Not instantiated directly by user.
在 3.4 版的變更: The strict parameter was removed. HTTP 0.9 style "Simple Responses" are no longer supported.
This module provides the following function:
- http.client.parse_headers(fp)¶
Parse the headers from a file pointer fp representing a HTTP request/response. The file has to be a
BufferedIOBasereader (i.e. not text) and must provide a valid RFC 5322 style header.This function returns an instance of
http.client.HTTPMessagethat holds the header fields, but no payload (the same asHTTPResponse.msgandhttp.server.BaseHTTPRequestHandler.headers). After returning, the file pointer fp is ready to read the HTTP body.備註
parse_headers()does not parse the start-line of a HTTP message; it only parses theName: valuelines. The file has to be ready to read these field lines, so the first line should already be consumed before calling the function.
The following exceptions are raised as appropriate:
- exception http.client.HTTPException¶
The base class of the other exceptions in this module. It is a subclass of
Exception.
- exception http.client.NotConnected¶
HTTPException的子類別。
- exception http.client.InvalidURL¶
A subclass of
HTTPException, raised if a port is given and is either non-numeric or empty.
- exception http.client.UnknownProtocol¶
HTTPException的子類別。
- exception http.client.