使用 python 连接到 Tor 上的 .onion 网站? [英] Connect to .onion websites on tor using python?

查看:32
本文介绍了使用 python 连接到 Tor 上的 .onion 网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到现在为止的代码

进口袜子进口插座进口请求导入jsonsocks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, addr="127.0.0.1", port=9050)socket.socket = 袜子.socksocket数据 = json.loads(requests.get("http://freegeoip.net/json/").text)

而且它工作正常.问题是当我使用 .onion url 它显示错误

<块引用>

无法建立新连接:[Errno -2] 名称或服务未知

经过一些研究,我发现虽然 http 请求是通过 tor 发出的,但解析仍然通过 clearnet 进行.什么是正确的方法,以便我也可以通过 Tor 网络解析域以连接到 .onion 网址?

解决方案

如果可能,尽量避免猴子补丁.如果您使用的是现代版本的请求,那么您应该已经拥有此功能.

导入请求导入json代理 = {'http': 'socks5h://127.0.0.1:9050','https': 'socks5h://127.0.0.1:9050'}data = requests.get("http://altaddresswcxlld.onion",proxies=proxies).text打印(数据)

使用 socks5h:// 方案指定代理很重要,以便通过 SOCKS 处理 DNS 解析,以便 Tor 可以正确解析 .onion 地址.>

Here is the code that i have till now

import socks
import socket
import requests
import json

socks.setdefaultproxy(proxy_type=socks.PROXY_TYPE_SOCKS5, addr="127.0.0.1", port=9050)
socket.socket = socks.socksocket

data = json.loads(requests.get("http://freegeoip.net/json/").text)

and it works fine. The problem is when i use a .onion url it shows error

Failed to establish a new connection: [Errno -2] Name or service not known

After researching a little i found that although the http request is made over tor the resolution still occours over clearnet. What is the proper way so i can also have the domain resolved over tor network to connect to .onion urls ?

解决方案

Try to avoid the monkey patching if possible. If you're using modern version of requests, then you should have this functionality already.

import requests
import json

proxies = {
    'http': 'socks5h://127.0.0.1:9050',
    'https': 'socks5h://127.0.0.1:9050'
}

data = requests.get("http://altaddresswcxlld.onion",proxies=proxies).text

print(data)

It's important to specify the proxies using the socks5h:// scheme so that DNS resolution is handled over SOCKS so Tor can resolve the .onion address properly.

这篇关于使用 python 连接到 Tor 上的 .onion 网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆