Skip to main content
The ProxyRotator class provides thread-safe proxy rotation with pluggable rotation strategies. It supports both string URLs and Playwright-style dictionary proxies.

Class Definition

Constructor

List[ProxyType]
required
List of proxy URLs or Playwright-style proxy dicts.String format: "http://proxy1:8080" or "http://user:pass@proxy:8080"Dict format: {"server": "http://proxy:8080", "username": "user", "password": "pass"}
RotationStrategy
default:"cyclic_rotation"
Rotation strategy function. Takes (proxies, current_index) and returns (proxy, next_index).Defaults to cyclic_rotation (sequential iteration with wraparound).
Raises:
  • ValueError if proxies list is empty
  • ValueError if proxy dict is missing “server” key
  • TypeError if strategy is not callable
  • TypeError if proxy is not str or dict

Methods

get_proxy

Get the next proxy according to the rotation strategy. Thread-safe. Returns: Proxy (str or dict) selected by the strategy Example:

Properties

proxies

Get a copy of all configured proxies. Returns: List of proxies (defensive copy)

Special Methods

Length

Return the total number of configured proxies. Example:

String Representation

Example:

Built-in Strategies

cyclic_rotation

Default cyclic rotation strategy - iterates through proxies sequentially, wrapping around at the end.
List[ProxyType]
required
List of available proxies.
int
required
Current position in the rotation.
Returns: Tuple of (selected_proxy, next_index)

Utility Functions

is_proxy_error

Check if an error is proxy-related. Works for both HTTP and browser errors.
Exception
required
Exception to check.
Returns: True if the error message contains proxy-related indicators Detected indicators:
  • “net::err_proxy”
  • “net::err_tunnel”
  • “connection refused”
  • “connection reset”
  • “connection timed out”
  • “failed to connect”
  • “could not resolve proxy”
Example:

Usage Examples

Basic Cyclic Rotation

Dictionary Proxies (Playwright-style)

Mixed Proxy Types

With Spider

Error Handling with Rotation

Custom Rotation Strategy

Weighted Rotation Strategy

Round-Robin with Skip

Thread Safety Demo

Type Definitions

See Also