Is there any way to use aiohttp client with socks proxy?
Asked Answered
Q

4

14

Looks like aiohttp.ProxyConnector doesn't support socks proxy. Is there any workaround for this? I would be grateful for any advice.

Qua answered 30/4, 2016 at 22:10 Comment(1)
You can try to ask your question in Google group for aio-libs: groups.google.com/forum/#!forum/aio-libsPrithee
I
9

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
Inanity answered 20/9, 2016 at 11:56 Comment(2)
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-usageFollansbee
D
2

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

Dacha answered 6/12, 2018 at 18:32 Comment(2)
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
B
2

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
Barnard answered 27/8, 2023 at 12:12 Comment(0)
B
1

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)
Beverleybeverlie answered 11/12, 2023 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.