ProxyProvider Shared Interface
- class proxyproviders.proxy_provider.ProxyConfig(refresh_interval: float = 600, timeout: float = 30, refresh_retry_delay: float = 30, failure_cooldown: float = 60)[source]
Bases:
objectProvides standard shared configuration options for all proxy providers.
Example:
from proxyproviders import Webshare, ProxyConfig config = ProxyConfig(refresh_interval=5) proxy_provider = Webshare(api_key="your-api-key", config=config)
- refresh_interval: float = 600
Duration in seconds after which the proxy list is refreshed to fetch new proxies. Set to 0 to disable automatic refreshing after the initial fetch.
- timeout: float = 30
Timeout in seconds for each request to the provider’s API, so a stalled connection raises instead of hanging forever.
- refresh_retry_delay: float = 30
After an automatic refresh fails and the previous proxy list is served instead, wait this many seconds before trying the provider’s API again.
- failure_cooldown: float = 60
How long, in seconds,
ProxyProvider.get_proxy()avoids a proxy after it was passed toProxyProvider.report_failure().
- class proxyproviders.proxy_provider.ProxyProvider(config: ProxyConfig | None = None)[source]
Bases:
ABCBase class for all proxy providers that provides shared functionality across all providers.
You’ll need to use a specific provider to fetch proxies and this shouldn’t directly be initialized. However, this class is useful for type hinting and for creating custom providers that are not included in the library, although consider contributing them to the library if you are making them.
- Parameters:
config – Configuration for the proxy provider. View the ProxyConfig class docs for more information.
- config: ProxyConfig
- list_proxies(force_refresh: bool = False) List[Proxy][source]
Returns the stored proxies, fetching them from the provider first when they are missing or older than
refresh_interval.Safe to call from several threads: when the list is stale only one thread fetches and the others wait for its result.
If an automatic refresh fails and an earlier list is available, that list is returned and a warning is logged, so one failed call to the provider’s API does not take your proxies away. The error is raised when there is nothing to fall back on, or when
force_refreshis True.- Parameters:
force_refresh – If True, fetches new proxies even if the list is not considered stale.
- get_proxy(algorithm: Algorithm | None = None) Proxy[source]
Get a single proxy using the specified selection algorithm.
- Parameters:
algorithm – Selection algorithm to use. If None, uses the provider’s default RoundRobin algorithm.
- Returns:
Selected proxy
- Raises:
ValueError – If no proxies are available
Example
>>> from proxyproviders import Webshare >>> from proxyproviders.algorithms import Random, RoundRobin >>> >>> provider = Webshare(api_key="your-key") >>> >>> # Uses default RoundRobin (cycles through proxies) >>> proxy = provider.get_proxy() >>> >>> # Uses Random selection >>> proxy = provider.get_proxy(Random()) >>> >>> # One-liner with requests >>> import requests >>> from proxyproviders.models.proxy import ProxyFormat >>> requests.get("https://httpbin.org/ip", proxies=provider.get_proxy().format(ProxyFormat.REQUESTS))
- report_failure(proxy: Proxy, cooldown: float | None = None) None[source]
Tell the provider a proxy just failed, so
get_proxy()avoids it for a while.Nothing calls this for you unless you use
RotatingSession; it is how your own code feeds back a connection error, a ban or a bad response. The proxy stays inlist_proxies()and comes back into rotation once the cooldown has passed. If every proxy is cooling down,get_proxy()still returns one.- Parameters:
proxy – The proxy that failed
cooldown – Seconds to avoid it for. Defaults to
ProxyConfig.failure_cooldown.
Example
>>> proxy = provider.get_proxy() >>> try: ... requests.get(url, proxies=proxy.format(ProxyFormat.REQUESTS), timeout=10) ... except requests.exceptions.ProxyError: ... provider.report_failure(proxy)
- report_success(proxy: Proxy) None[source]
Clear a failure reported for a proxy, putting it back into rotation right away.
- available_proxies() List[Proxy][source]
Like
list_proxies(), without the proxies currently cooling down afterreport_failure().