python-3.x - 如何使用 python aiohttp 连接到.onion 站点?

标签 python-3.x python-asyncio tor aiohttp socks

我正在尝试使用 python 连接到 .onion 站点。我必须在端口 9050 上运行,但出现以下错误:

  Traceback (most recent call last):
  File "/Users/jane/code/test/test.py", line 15, in main
    res = await fetch(session, id)
  File "/Users/jane/code/test/test.py", line 9, in fetch
    async with session.get(url) as res:
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 1005, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.7/site-packages/aiohttp/client.py", line 476, in _request
    timeout=real_timeout
  File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 522, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 854, in _create_connection
    req, traces, timeout)
  File "/usr/local/lib/python3.7/site-packages/aiohttp/connector.py", line 959, in _create_direct_connection
    raise ClientConnectorError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host intelex7ny6coqno.onion:80 ssl:None [nodename nor servname provided, or not known]

代码:

import asyncio
import aiohttp
from aiohttp_socks import SocksConnector

async def fetch(session, id):
    print('Starting {}'.format(id))
    url = 'http://intelex7ny6coqno.onion/topic/{}'.format(id)
    async with session.get(url) as res:
        return res.text

async def main(id):
    connector = SocksConnector.from_url('socks5://localhost:9050')
    async with aiohttp.ClientSession(connector=connector) as session:
        res = await fetch(session, id)
        print(res)

if __name__ == '__main__':
    ids = ['10', '11', '12']
    loop = asyncio.get_event_loop()

    future = [asyncio.ensure_future(main(id)) for id in ids]
    loop.run_until_complete(asyncio.wait(future))

这段代码工作正常:

import requests
session = requests.session()
session.proxies['http'] = 'socks5h://localhost:9050'
session.proxies['https'] = 'socks5h://localhost:9050'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
}
res = session.get(url, headers=headers)
print(res)

为什么我得到 Cannot connect to host intelex7ny6coqno.onion:80 ssl:None [nodename nor servname provided, or not known]

我在这里错过了什么?

最佳答案

默认情况下,它似乎使用本地 DNS 解析器异步解析主机名。使用请求 socks5h 时,您将通过 SOCKS (Tor) 获得 DNS 解析。

添加rdns =True 似乎适用于 .onion 地址:

connector = SocksConnector.from_url('socks5://localhost:9050', rdns=True)

关于python-3.x - 如何使用 python aiohttp 连接到.onion 站点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55191914/

相关文章:

python - 为什么在 Pandas Series 上调用 .sort() 函数会就地对其值进行排序并且什么都不返回?

routing - 使用 Tor 代理作为网络网关的正确 iptables 规则是什么?

ubuntu - 带代理的 Bazaar CVS (bzr)(使用 tor 的 socks )

python - pyinstaller在exe文件中添加包含图像的文件夹

python-3.x - 等待分离的容器运行

python - `asyncio.new_event_loop` 有哪些用例?

python - 如果不等待异步函数会发生什么?

linux - 在 centOS 7 服务器上安装 TOR

python - 比较python中的日期字符串

postgresql - py.test 混合装置和 asyncio 协程