Looks like aiohttp.ProxyConnector
doesn't support socks proxy. Is there any workaround for this? I would be grateful for any advice.
Is there any way to use aiohttp client with socks proxy?
You can try to ask your question in Google group for aio-libs: groups.google.com/forum/#!forum/aio-libs –
Prithee
Have you tried aiosocks ?
import asyncio
import aiosocks
from aiosocks.connector import SocksConnector
conn = SocksConnector(proxy=aiosocks.Socks5Addr(PROXY_ADDRESS, PROXY_PORT), proxy_auth=None, remote_resolve=True)
session = aiohttp.ClientSession(connector=conn)
async with session.get('http://python.org') as resp:
assert resp.status == 200
When I try this, I get
"ImportError: cannot import name 'SocksConnector'"
... What is correct way to do this now? –
Unhandy @J.Taylor Use
ProxyConnector
instead. from aiosocks.connector import ProxyConnector
github.com/nibrag/aiosocks#aiohttp-usage –
Follansbee aiosocks does not work with the newer version 3.+ of aiohttp. You can use aiosocksy to implement socks proxy.
To check whether aiosocksy is working you can look at the following code sample https://mcmap.net/q/830849/-how-to-use-socks-proxies-to-make-requests-with-aiohttp
While you are correct,
aiohttp-socks
from same developer is more likely to get maintenance in future updates, according to readme. –
Glowing as above user mentioned, now
aiohttp-socks
is supported again, and aiosocksy
was deprecated. –
Beverleybeverlie The Correct answer doesn't work anymore.
Import ProxyConnector from aiosocks.connector rather than SocksConnector
import aiohttp
import aiosocks
from aiosocks.connector import ProxyConnector
conn = ProxyConnector(remote_resolve=True)
async with aiohttp.ClientSession(connector=conn) as session:
async with session.get("http://python.org", proxy=f"{PROXY_ADDRESS}:{PROXY_PORT}") as resp:
assert resp.status == 200
in new versions, you might try something like this:
from aiohttp_socks import ProxyConnector
asyncio_loop = asyncio.get_running_loop()
# context = ssl.create_default_context(cafile=...)
connector = ProxyConnector.from_url(
socksProxy,
# ssl=context,
loop=asyncio_loop,
enable_cleanup_closed=True
)
# override session
session = aiohttp.ClientSession(loop=asyncio_loop, connector=connector, trust_env=False)
encoded_body = ({'param1': 'value1'}).encode()
session.get(yarl.URL(url, encoded=True), data=encoded_body, headers=request_headers, timeout=10) as response:
http_response = await response.text(errors='replace')
print(http_response)
© 2022 - 2024 — McMap. All rights reserved.