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).ValueErrorif proxies list is emptyValueErrorif proxy dict is missing “server” keyTypeErrorif strategy is not callableTypeErrorif proxy is not str or dict
Methods
get_proxy
Properties
proxies
Special Methods
Length
String Representation
Built-in Strategies
cyclic_rotation
List[ProxyType]
required
List of available proxies.
int
required
Current position in the rotation.
(selected_proxy, next_index)
Utility Functions
is_proxy_error
Exception
required
Exception to check.
- “net::err_proxy”
- “net::err_tunnel”
- “connection refused”
- “connection reset”
- “connection timed out”
- “failed to connect”
- “could not resolve proxy”
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
- Spider - Using proxies in spiders
- Request - Passing proxy to requests
- SessionManager - Session-level proxy configuration