requests / aiohttp with SOCKS5. 0x05: Connection refused

442 Views Asked by At

I have two versions of the program:

  • Synchronous, using requests library;
  • Asynchronous, using aiohttp library.

It is necessary to make requests to a site using SOCKS5 proxy. After installing the necessary libraries to work with SOCKS5 proxies, I tried making requests to a few sites. Everything worked fine with all sites except the necessary one. When sending a request to it, an error appeared:

0x05: Connection refused

1

There are 1 best solutions below

0
On BEST ANSWER

I found the solution for the requests library in a couple minutes. But I didn't find a solution for aiohttp, so it took me several hours to find a solution on my own.

I finally found a solution, so I'm going to publish solutions for each of the libraries below to help other developers save time in solving the same problem.


To make a successful request to such a site try to enable reverse DNS (rDNS). I will describe how to do it for requests and aiohttp libraries.

requests

To enable rDNS, you need to use socks5h://... instead of socks5://... in a proxy URL.

requirements.txt

PySocks==1.7.1
requests==2.31.0

main.py

import requests


def main():
    proxy = 'socks5://username:[email protected]:1111'
    proxy = proxy.replace('socks5://', 'socks5h://')
    
    # or just 
    # proxy = 'socks5h://username:[email protected]:1111'

    response = requests.get(url='https://www.example.com/', proxies={'http': proxy, 'https': proxy})
    print(response.text)


if __name__ == '__main__':
    main()

aiohttp

To enable rDNS, you need to pass the rdns=True parameter during initialization of the ProxyConnector.

requirements.txt

aiohttp==3.8.4
aiohttp-socks==0.8.0
PySocks==1.7.1

main.py

import asyncio

import aiohttp
from aiohttp_socks import ProxyConnector


async def main():
    proxy = 'socks5://username:[email protected]:1111'
    connector = ProxyConnector.from_url(url=proxy, rdns=True)
    async with aiohttp.ClientSession(connector=connector) as session:
        async with session.get(url='https://www.example.com/') as response:
            print(await response.text())


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())