The aiohttp-socks package provides a proxy connector for aiohttp.
Supports SOCKS4(a), SOCKS5(h), HTTP (CONNECT) as well as Proxy chains.
It uses python-socks for core proxy functionality.
- Python >= 3.9
- aiohttp >= 3.10.0
- python-socks[asyncio] >= 2.4.3
pip install aiohttp_socks
importaiohttpfromaiohttp_socksimportProxyType, ProxyConnector, ChainProxyConnectorasyncdeffetch(url):
connector=ProxyConnector.from_url('socks5://user:password@127.0.0.1:1080')
### or use ProxyConnector constructor# connector = ProxyConnector(# proxy_type=ProxyType.SOCKS5,# host='127.0.0.1',# port=1080,# username='user',# password='password',# rdns=True # default is True for socks5# )### proxy chaining (since ver 0.3.3)# connector = ChainProxyConnector.from_urls([# 'socks5://user:password@127.0.0.1:1080',# 'socks4://127.0.0.1:1081',# 'http://user:password@127.0.0.1:3128',# ])asyncwithaiohttp.ClientSession(connector=connector) assession:
asyncwithsession.get(url) asresponse:
returnawaitresponse.text()Unlike aiosocksy, aiohttp_socks has only single point of integration with aiohttp. This makes it easier to maintain compatibility with new aiohttp versions.