requests / aiohttp with SOCKS5. 0x05: 连接被拒绝

huangapple go评论69阅读模式
英文:

requests / aiohttp with SOCKS5. 0x05: Connection refused

问题

我有两个程序版本:

  • 同步版本,使用 requests 库;
  • 异步版本,使用 aiohttp 库。

需要使用 SOCKS5 代理向一个站点发起请求。在安装了与 SOCKS5 代理一起使用的必要库后,我尝试向一些站点发送请求。对于所有站点,除了必要的那个之外,一切正常运行。在向它发送请求时,出现了一个错误:

> 0x05:连接被拒绝

英文:

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

得分: 1

我找到了requests库的解决方案,只用了几分钟。但是我没有找到aiohttp的解决方案,所以花了我好几个小时才自己找到了解决方案。

最终,我找到了解决方案,所以我将在下面为每个库发布解决方案,以帮助其他开发人员节省解决相同问题的时间。


要成功请求这样的网站,请尝试启用反向DNS(rDNS)。我将描述如何在requestsaiohttp库中执行此操作。

requests

要启用rDNS,您需要在代理URL的初始化中使用socks5h://...,而不是socks5://...

requirements.txt

PySocks==1.7.1
requests==2.31.0

main.py

import requests


def main():
    proxy = 'socks5://username:password@0.0.0.0:1111'
    proxy = proxy.replace('socks5://', 'socks5h://')
    
    # 或者只需
    # proxy = 'socks5h://username:password@0.0.0.0:1111'

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


if __name__ == '__main__':
    main()

aiohttp

要启用rDNS,您需要在ProxyConnector的初始化期间传递rdns=True参数。

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:password@0.0.0.0: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())

英文:

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:password@0.0.0.0:1111'
    proxy = proxy.replace('socks5://', 'socks5h://')
    
    # or just 
    # proxy = 'socks5h://username:password@0.0.0.0: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:password@0.0.0.0: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())

huangapple
  • 本文由 发表于 2023年7月11日 02:51:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定