Connect to .onion websites on tor using python?

天大地大妈咪最大 提交于 2019-12-03 18:10:25

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/43682909/connect-to-onion-websites-on-tor-using-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!