Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

aiohttp

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

like image 823
Mikhail Gerasimov Avatar asked Apr 30 '16 22:04

Mikhail Gerasimov


People also ask

Is Aiohttp better than requests?

get is that requests fetches the whole body of the response at once and remembers it, but aiohttp doesn't. aiohttp lets you ignore the body, or read it in chunks, or read it after looking at the headers/status code. That's why you need to do a second await : aiohttp needs to do more I/O to get the response body.

What is client session Aiohttp?

Client session is the recommended interface for making HTTP requests. Session encapsulates a connection pool (connector instance) and supports keepalives by default.

How do I pass headers in Aiohttp?

If you need to add HTTP headers to a request, pass them in a dict to the headers parameter. await session. post(url, data='Привет, Мир! ')


2 Answers

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
like image 109
Thomas Perrot Avatar answered Oct 10 '22 20:10

Thomas Perrot


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://stackoverflow.com/a/53657536/6735546

like image 22
Monu Yadav Avatar answered Oct 10 '22 20:10

Monu Yadav