跳到主要内容
版本:1.2.2

SafePoolManager

私有上下文源码

with SafePoolManager(
num_pools: int = 10,
headers: dict[str, str] | None = None,
**connection_pool_kw
) as pool:
pool: SafePoolManager

支持上下文的urllib3.PoolManager的封装。

危险

这是私有类,用户不应使用它。

参数

参见

https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html

方法

request

req: SafeRequest[urllib3.BaseHTTPResponse] = pool.request(
method: MethodApproved,
url: str,
fields: Mapping[str, str | bytes | ReqFile] | None = None,
headers: Mapping[str, str] | None = None,
**urlopen_kw,
)

修改过的PoolManager.request(...)

该方法提供了原urllib3不曾提供的类型注解。其返回值是封装版本SafeRequest,用以提供进一步的上下文,而非一个简单的HTTPResponse对象。

输入

ArgumentTypeRequired
Description
methodMethodApproved请求所用的HTTP方法。
urlstr请求的目标URL。
fieldsMapping[str, str | bytes | ReqFile] | None要发送的多表单域,可以留空。
headersMapping[str, str] | None请求的HTTP头。
**urlopen_kw-其他要传递给urlopen(...)的参数。

输出

ArgumentType
Description
reqSafeRequest[urllib3.BaseHTTPResponse]SafeRequest上下文对象。进入该上下文将提供该方法所返回的、被封装的HTTPResponse

其他方法

其他方法参见

https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html

范例

使用上下文管理连接

use_pool_context.py
from syncstream.webtools import SafePoolManager


with SafePoolManager() as pool:
with pool.request("get", "https://google.com") as req:
# req已经由SafeRequest管理了。
print(req.status)