22.12. http.client — HTTP protocol client

Source code: Lib/http/client.py


This module defines classes which 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).

The module provides the following classes:

class http.client.HTTPConnection(host, port=None, [timeout, ]source_address=None, blocksize=8192)

An HTTPConnection instance represents one transaction with an HTTP server. It should be instantiated 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 form host: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 was added.

3.4 版更變: The strict parameter was removed. HTTP 0.9-style 「Simple Responses」 are not longer supported.

3.7 版更變: blocksize parameter was added.

class http.client.HTTPSConnection(host, port=None, key_file=None, cert_file=None, [timeout, ]source_address=None, *, context=None, check_hostname=None, blocksize=8192)

A subclass of HTTPConnection that uses SSL for communication with secure servers. Default port is 443. If context is specified, it must be a ssl.SSLContext instance describing the various SSL options.

Please read Security considerations for more information on best practices.

3.2 版更變: source_address, context and check_hostname were added.

3.2 版更變: This class now supports HTTPS virtual hosts if possible (that is, if ssl.HAS_SNI is 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.6 版後已棄用: key_file and cert_file are deprecated in favor of context. Please use ssl.SSLContext.load_cert_chain() instead, or let ssl.create_default_context() select the system’s trusted CA certificates for you.

The check_hostname parameter is also deprecated; the ssl.SSLContext.check_hostname attribute of context should be used instead.

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.

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

A subclass of 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

A subclass of HTTPException.

exception http.client.UnknownTransferEncoding

A subclass of HTTPException.

exception http.client.UnimplementedFileMode

A subclass of HTTPException.

exception http.client.IncompleteRead

A subclass of HTTPException.

exception http.client.ImproperConnectionState

A subclass of HTTPException.

exception http.client.CannotSendRequest

A subclass of ImproperConnectionState.

exception http.client.CannotSendHeader

A subclass of ImproperConnectionState.

exception http.client.ResponseNotReady

A subclass of ImproperConnectionState.

exception http.client.BadStatusLine

A subclass of