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: Session

A requests.Session 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, the proxy is reported with report_failure() and the request is tried again on the next one, up to max_retries more times. It works with any provider, including ProxyList.

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, HEAD and OPTIONS only. Other methods may already have been acted on, so the error is raised instead.

  • A 407 Proxy Authentication Required response, or any status you list in retry_statuses: retried for any method.

Anything else, including ordinary error statuses such as 404 or 500, is returned or raised exactly as requests would. 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 in retry_statuses or call report_failure yourself.

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)
last_proxy: Proxy | None

The proxy used for the most recent request, whether it succeeded or not.

request(method: str | bytes, url: str | bytes, *args: Any, **kwargs: Any) → Response[source]

Send a request through a proxy from the provider, retrying on the next proxy if this one fails. Takes the same arguments as requests.Session.request.

send(request: PreparedRequest, **kwargs: Any) → Response[source]

Send a prepared request. Rotates like request() unless proxies is given.

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

An 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 to max_retries more 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, HEAD and OPTIONS only

  • a 407 Proxy Authentication Required response, or any status you list in retry_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= or http2=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")
handle_request(request: Request) → Response[source]

Send the request through a proxy, moving to the next proxy if it fails.

class proxyproviders.httpx_transport.AsyncRotatingTransport(*args: Any, **kwargs: Any)[source]

Bases: _Rotation, AsyncBaseTransport

The async version of RotatingTransport, for httpx.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")
async handle_async_request(request: Request) → Response[source]

Send the request through a proxy, moving to the next proxy if it fails.