Rotating Session
An opt-in requests.Session that picks a proxy for every request.
Nothing else in the package uses this module. Import it when you want rotation and
retries handled for you instead of calling provider.get_proxy() yourself.
- class proxyproviders.session.RotatingSession(provider: ProxyProvider, algorithm: Algorithm | None = None, max_retries: int = 2, retry_statuses: Collection[int] = ())[source]
Bases:
SessionA
requests.Sessionthat routes each request through a proxy from a provider.Every request asks the provider for a proxy. If the request fails to get through the proxy, the proxy is reported with
report_failure()and the request is tried again on the next one, up tomax_retriesmore times. It works with any provider, includingProxyList.What counts as a failed proxy, and when the request is sent again:
Failing to connect to the proxy, or to open a tunnel through it: retried for any HTTP method, because the request never reached the target.
Any other connection error or timeout, such as a proxy that drops the connection or stops responding: retried for
GET,HEADandOPTIONSonly. Other methods may already have been acted on, so the error is raised instead.A
407 Proxy Authentication Requiredresponse, or any status you list inretry_statuses: retried for any method.
Anything else, including ordinary error statuses such as 404 or 500, is returned or raised exactly as
requestswould. A TLS error is raised without blaming the proxy, since it usually comes from the target. A slow target cannot be told apart from a slow proxy, so a read timeout counts against the proxy. Deciding that a response means “banned” is left to you: list statuses inretry_statusesor callreport_failureyourself.A request made with an explicit
proxies=argument is sent as given, with no rotation. Requests that upload files or stream their body are sent once and never retried, since the body cannot be read twice.- Parameters:
provider – Where proxies come from
algorithm – Selection algorithm, as for
provider.get_proxy(). Defaults to the provider’s round robin.max_retries – How many more proxies to try after the first one fails
retry_statuses – Extra response status codes treated as a failed proxy, e.g.
{403, 429}for a target that blocks proxies. 407 is always included.
Example:
from proxyproviders import Webshare from proxyproviders.session import RotatingSession session = RotatingSession(Webshare(api_key="your-api-key"), max_retries=3) response = session.get("https://httpbin.org/ip", timeout=10) print(response.json(), session.last_proxy)
Rotating httpx Transports
Opt-in httpx transports that pick a proxy for every request, sync and async.
Nothing else in the package uses this module, and it is the only one that needs httpx:
pip install "proxyproviders[httpx]".
- class proxyproviders.httpx_transport.RotatingTransport(*args: Any, **kwargs: Any)[source]
Bases:
_Rotation,BaseTransportAn httpx transport that routes each request through a proxy from a provider.
Every request asks the provider for a proxy. If the request fails to get through, the proxy is reported with
report_failure()and the request is sent again through the next one, up tomax_retriesmore times.What counts as a failed proxy:
a connect error, connect timeout or proxy error: retried for any HTTP method, because the request never reached the target
a read or write error or timeout, or a broken response: retried for
GET,HEADandOPTIONSonlya
407 Proxy Authentication Requiredresponse, or any status you list inretry_statuses: retried for any method
Anything else is returned or raised exactly as httpx would. httpx reports a failed TLS handshake with the target as a connect error, so that also counts against the proxy. Requests with a streamed or multipart body are sent once and not retried. One connection pool is kept per proxy used, so connections are reused when a proxy comes round again.
- Parameters:
provider – Where proxies come from
algorithm – Selection algorithm, as for
provider.get_proxy()max_retries – How many more proxies to try after the first one fails
retry_statuses – Extra response status codes treated as a failed proxy, e.g.
{403, 429}. 407 is always included.transport_options – Passed to each underlying
httpx.HTTPTransport, e.g.verify=orhttp2=True
Example:
import httpx from proxyproviders import Webshare from proxyproviders.httpx_transport import RotatingTransport transport = RotatingTransport(Webshare(api_key="your-api-key"), max_retries=3) with httpx.Client(transport=transport) as client: response = client.get("https://httpbin.org/ip")
- class proxyproviders.httpx_transport.AsyncRotatingTransport(*args: Any, **kwargs: Any)[source]
Bases:
_Rotation,AsyncBaseTransportThe async version of
RotatingTransport, forhttpx.AsyncClient.It behaves the same way and takes the same arguments. Asking the provider for a proxy runs in a worker thread, so a provider refreshing its list from the vendor’s API does not block the event loop.
Example:
import httpx from proxyproviders import Webshare from proxyproviders.httpx_transport import AsyncRotatingTransport transport = AsyncRotatingTransport(Webshare(api_key="your-api-key")) async with httpx.AsyncClient(transport=transport) as client: response = await client.get("https://httpbin.org/ip")