Proxy connector for aiohttp supporting SOCKS4/5 and HTTP proxies with proxy chaining capabilities
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Sequential proxy routing through multiple proxy servers for enhanced anonymity and flexible network routing. Allows routing traffic through a chain of proxies, with each proxy in the chain connecting to the next.
Connector class that routes connections through multiple proxies in sequence, supporting mixed proxy types and configurations.
class ChainProxyConnector:
def __init__(self, proxy_infos: Iterable[ProxyInfo], **kwargs):
"""
Create a chained proxy connector for aiohttp.
Args:
proxy_infos (Iterable[ProxyInfo]): Sequence of proxy configurations
**kwargs: Additional arguments passed to TCPConnector
"""import aiohttp
from aiohttp_socks import ChainProxyConnector, ProxyInfo, ProxyType
# Create proxy chain with ProxyInfo objects
proxy_chain = [
ProxyInfo(
proxy_type=ProxyType.SOCKS5,
host='first-proxy.example.com',
port=1080,
username='user1',
password='pass1'
),
ProxyInfo(
proxy_type=ProxyType.SOCKS4,
host='second-proxy.example.com',
port=1081
),
ProxyInfo(
proxy_type=ProxyType.HTTP,
host='third-proxy.example.com',
port=3128,
username='user3',
password='pass3'
)
]
connector = ChainProxyConnector(proxy_chain)
# Use with aiohttp
async with aiohttp.ClientSession(connector=connector) as session:
async with session.get('http://httpbin.org/ip') as response:
data = await response.json()
print(data)Create proxy chains directly from lists of proxy URL strings for convenient configuration.
@classmethod
def from_urls(cls, urls: Iterable[str], **kwargs: Any) -> 'ChainProxyConnector':
"""
Create ChainProxyConnector from list of proxy URLs.
Args:
urls (Iterable[str]): Sequence of proxy URLs
**kwargs: Additional arguments passed to constructor
Returns:
ChainProxyConnector: Configured chain connector instance
Each URL format: protocol://[username:password@]host:port
"""from aiohttp_socks import ChainProxyConnector
# Simple proxy chain from URLs
proxy_urls = [
'socks5://user:password@first-proxy.example.com:1080',
'socks4://second-proxy.example.com:1081',
'http://user:password@third-proxy.example.com:3128'
]
connector = ChainProxyConnector.from_urls(proxy_urls)
# Mixed proxy types in chain
mixed_chain = [
'socks5://127.0.0.1:1080', # Local SOCKS5
'socks4a://proxy.example.com:1081', # Remote SOCKS4a
'http://proxy.example.com:3128' # HTTP proxy
]
connector = ChainProxyConnector.from_urls(mixed_chain)Data structure for specifying proxy configuration in chains, providing all necessary connection parameters.
class ProxyInfo(NamedTuple):
proxy_type: ProxyType
host: str
port: int
username: Optional[str] = None
password: Optional[str] = None
rdns: Optional[bool] = None
"""
Proxy configuration information.
Attributes:
proxy_type (ProxyType): Type of proxy (SOCKS4/4a/5/5h/HTTP)
host (str): Proxy server hostname or IP address
port (int): Proxy server port number
username (str, optional): Username for proxy authentication
password (str, optional): Password for proxy authentication
rdns (bool, optional): Enable remote DNS resolution
"""from aiohttp_socks import ProxyInfo, ProxyType
# Basic proxy info
proxy1 = ProxyInfo(
proxy_type=ProxyType.SOCKS5,
host='proxy.example.com',
port=1080
)
# Authenticated proxy with remote DNS
proxy2 = ProxyInfo(
proxy_type=ProxyType.SOCKS5,
host='secure-proxy.example.com',
port=1080,
username='myuser',
password='mypass',
rdns=True
)
# HTTP proxy
proxy3 = ProxyInfo(
proxy_type=ProxyType.HTTP,
host='http-proxy.example.com',
port=3128,
username='httpuser',
password='httppass'
)
# Create chain from ProxyInfo objects
chain = [proxy1, proxy2, proxy3]
connector = ChainProxyConnector(chain)When using proxy chains, traffic flows through proxies in the order specified:
This creates a tunnel through multiple proxy servers, with each proxy only knowing about its immediate predecessor and successor in the chain. This provides enhanced privacy and allows routing through different networks or geographic locations.
Install with Tessl CLI
npx tessl i tessl/pypi-aiohttp-socks