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
Single proxy connection functionality providing support for SOCKS4, SOCKS4a, SOCKS5, SOCKS5h, and HTTP tunneling proxies with authentication and SSL support.
The main connector class for single proxy connections, extending aiohttp's TCPConnector with proxy support.
class ProxyConnector:
def __init__(
self,
host: str,
port: int,
proxy_type: ProxyType = ProxyType.SOCKS5,
username: Optional[str] = None,
password: Optional[str] = None,
rdns: Optional[bool] = None,
proxy_ssl: Optional[SSLContext] = None,
**kwargs: Any
) -> None:
"""
Create a proxy connector for aiohttp.
Args:
host (str): Proxy server hostname or IP address
port (int): Proxy server port number
proxy_type (ProxyType): Type of proxy (SOCKS4/4a/5/5h/HTTP)
username (str, optional): Username for proxy authentication
password (str, optional): Password for proxy authentication
rdns (bool, optional): Enable remote DNS resolution (default True for SOCKS5)
proxy_ssl (SSLContext, optional): SSL context for proxy connection
**kwargs: Additional arguments passed to TCPConnector
"""import aiohttp
from aiohttp_socks import ProxyConnector, ProxyType
# Basic SOCKS5 proxy
connector = ProxyConnector(
proxy_type=ProxyType.SOCKS5,
host='127.0.0.1',
port=1080
)
# Authenticated SOCKS5 proxy with remote DNS
connector = ProxyConnector(
proxy_type=ProxyType.SOCKS5,
host='proxy.example.com',
port=1080,
username='user',
password='pass',
rdns=True
)
# HTTP proxy with SSL context
import ssl
ssl_context = ssl.create_default_context()
connector = ProxyConnector(
proxy_type=ProxyType.HTTP,
host='proxy.example.com',
port=3128,
proxy_ssl=ssl_context
)
# 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 connectors directly from proxy URL strings for convenient configuration.
@classmethod
def from_url(cls, url: str, **kwargs: Any) -> 'ProxyConnector':
"""
Create ProxyConnector from proxy URL.
Args:
url (str): Proxy URL in format: protocol://[username:password@]host:port
**kwargs: Additional arguments passed to constructor
Returns:
ProxyConnector: Configured connector instance
Supported URL formats:
- socks4://proxy.example.com:1080
- socks4a://user:pass@proxy.example.com:1080
- socks5://user:pass@proxy.example.com:1080
- socks5h://proxy.example.com:1080
- http://user:pass@proxy.example.com:3128
"""from aiohttp_socks import ProxyConnector
# Various proxy URL formats
connectors = [
ProxyConnector.from_url('socks5://127.0.0.1:1080'),
ProxyConnector.from_url('socks5://user:password@proxy.example.com:1080'),
ProxyConnector.from_url('socks4a://proxy.example.com:1080'),
ProxyConnector.from_url('http://user:password@proxy.example.com:3128'),
]
# Use with custom TCPConnector arguments
connector = ProxyConnector.from_url(
'socks5://proxy.example.com:1080',
limit=30,
ttl_dns_cache=60
)The library supports all major proxy protocols through the ProxyType enumeration:
Each proxy type has specific capabilities for authentication and DNS resolution handling.
Install with Tessl CLI
npx tessl i tessl/pypi-aiohttp-socks