无法通过python连接到Tor网站

时间:2018-10-16 15:17:25

标签: python tor

我想用python解析洋葱网站,想出了那个代码:

import requests

proxies = {
    'http': 'socks5://localhost:9150',
    'https': 'socks5://localhost:9150'
}
url = 'http://sblib3fk2gryb46d.onion'

print(requests.get(url, proxies=proxies).text)

给我一​​个错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 851, in connect
    negotiate(self, dest_addr, dest_port)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 497, in _negotiate_SOCKS5
    self, CONNECT, dest_addr)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 565, in _SOCKS5_request
    resolved = self._write_SOCKS5_address(dst, writer)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 621, in _write_SOCKS5_address
    socket.AI_ADDRCONFIG)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

在clearnet网站上工作正常。 Tor已打开,我使用mac os。 我已经尝试了几种选择,这是迄今为止唯一可行的选择(至少我设法通过tor连接到clearnet)

也许有人以前做过这样的事,可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

为了解析DNS并且还能够连接到.onion地址,您需要使用socks5h协议来告诉请求通过Tor解析DNS的请求。上面的示例只是socks5,这意味着它正在尝试使用常规DNS解析器来解析无法使用的.onion地址。

要解决此问题,请将您的proxies更改为:

proxies = {
    'http': 'socks5h://localhost:9150',
    'https': 'socks5h://localhost:9150'
}

此外,对于以后的读者来说,Tor浏览器捆绑包使用端口9150。要使用Tor守护程序,请尝试使用端口9050。