如何使用python aiohttp连接到.onion网站?

编程入门 行业动态 更新时间:2024-10-26 11:19:06
本文介绍了如何使用python aiohttp连接到.onion网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用python连接到 .onion 网站。我在端口9050上运行了Tor,并且收到以下错误:

I am trying to connect to a .onion site using python. I have tor running on port 9050 and I am getting the following error:

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 = '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)

为什么我得到无法连接到主机intelex7ny6coqno.onion:80 ssl:None [无节点名或服务名,或未知)?

我在这里想念什么?

推荐答案

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

By default it appears to be using the local DNS resolver to asynchronously resolve hostnames. When using requests socks5h you are getting DNS resolution over SOCKS (Tor).

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

Adding rdns=True appears to work for .onion addresses:

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

更多推荐

如何使用python aiohttp连接到.onion网站?

本文发布于:2023-11-23 10:18:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   如何使用   网站   aiohttp   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!