Skip to main content
Version: 1.2.2

SafePoolManager

ClassPrivateContextSource

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

A wrapped urllib3.PoolManager with context supported.

danger

This is a private class. Should not be used by users.

Arguments

See

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

Methods

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

Modified version of the PoolManager.request(...)

This method provide the typehints that are not available in the original urllib3 pacakge. The returned value is a wrapped SafeRequest ready for used as a context, not simply an HTTPResponse object.

Requires

ArgumentTypeRequired
Description
methodMethodApprovedThe HTTP method of the request.
urlstrThe target URL of this request.
fieldsMapping[str, str | bytes | ReqFile] | NoneThe multi-form fields to be sent, can be empty.
headersMapping[str, str] | NoneThe HTTP headers of this request.
**urlopen_kw-The other keywords will be forwarded to urlopen(...).

Returns

ArgumentType
Description
reqSafeRequest[urllib3.BaseHTTPResponse]A SafeRequest context object. Entering this context will provide the wrapped HTTPResponse returned by this method.

Other methods

Other methods can be referred here:

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

Example

Use context to manage connections

use_pool_context.py
from syncstream.webtools import SafePoolManager


with SafePoolManager() as pool:
with pool.request("get", "https://google.com") as req:
# req is already managed by SafeRequest.
print(req.status)