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
Argument | Type | Required | |
---|---|---|---|
method | MethodApproved | The HTTP method of the request. | |
url | str | The target URL of this request. | |
fields | Mapping[str, str | bytes | ReqFile] | None | The multi-form fields to be sent, can be empty. | |
headers | Mapping[str, str] | None | The HTTP headers of this request. | |
**urlopen_kw | - | The other keywords will be forwarded to urlopen(...) . |
Returns
Argument | Type | |
---|---|---|
req | SafeRequest[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
- Codes
- Results
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)
200