http.client — HTTP 协议客户端

源代码: Lib/http/client.py


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 使用它来处理使用 HTTP 和 HTTPS 的 URL。

另请参阅

The Requests 包 推荐为更高级别的 HTTP 客户端接口。

注意

HTTPS support is only available if Python was compiled with SSL support (through the ssl 模块)。

可用性 :非 WASI。

This module does not work or is not available on WebAssembly. See WebAssembly 平台 了解更多信息。

模块提供下列类:

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 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 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)