无法使用python连接到* .onion站点(<urlopen error =“”[errno =“”11001] =“”getaddrinfo =“”failed =“”>)

时间:2016-11-13 23:38:17

标签: python proxy tor socks

我正在尝试使用python访问* .onion网站。但是还没有成功。 我已经阅读了很多stackoverflow问题和答案,尝试了很多不同的方法来解决这个问题:我尝试使用Python 2.7Python 3.5,尝试使用urllib,{{1} },urllib2(然后我发现请求不适用于socks),requests等,但似乎没有任何效果。 现在我正处于我只得到以下错误的地方:

pysocks

不,我没有防火墙,是的,我有一个很好的互联网连接,是的,该网站确实存在。 我认为问题在于它是一个* .onion链接。

这就是我现在正在做的事情:

> <urlopen error [Errno 11001] getaddrinfo failed>

这就是我得到的:

import socks
import socket
import urllib
import urllib.request

socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket
r = urllib.request.urlopen("http://xmh57jrzrnw6insl.onion")
r.read()

我对这些东西都很陌生,所以我可能会错过一些非常简单的部分。但我会感激任何帮助。

ps:当试图访问不是* .onion网站时,我得到以下内容:

---------------------------------------------------------------------------
gaierror                                  Traceback (most recent call last)
C:\Users\yella\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
   1239             try:
-> 1240                 h.request(req.get_method(), req.selector, req.data, headers)
   1241             except OSError as err: # timeout error

C:\Users\yella\Anaconda3\lib\http\client.py in request(self, method, url, body, headers)
   1082         """Send a complete request to the server."""
-> 1083         self._send_request(method, url, body, headers)
   1084 

C:\Users\yella\Anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers)
   1127             body = body.encode('iso-8859-1')
-> 1128         self.endheaders(body)
   1129 

C:\Users\yella\Anaconda3\lib\http\client.py in endheaders(self, message_body)
   1078             raise CannotSendHeader()
-> 1079         self._send_output(message_body)
   1080 

C:\Users\yella\Anaconda3\lib\http\client.py in _send_output(self, message_body)
    910 
--> 911         self.send(msg)
    912         if message_body is not None:

C:\Users\yella\Anaconda3\lib\http\client.py in send(self, data)
    853             if self.auto_open:
--> 854                 self.connect()
    855             else:

C:\Users\yella\Anaconda3\lib\http\client.py in connect(self)
    825         self.sock = self._create_connection(
--> 826             (self.host,self.port), self.timeout, self.source_address)
    827         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

C:\Users\yella\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address)
    692     err = None
--> 693     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    694         af, socktype, proto, canonname, sa = res

C:\Users\yella\Anaconda3\lib\socket.py in getaddrinfo(host, port, family, type, proto, flags)
    731     addrlist = []
--> 732     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    733         af, socktype, proto, canonname, sa = res

gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

URLError                                  Traceback (most recent call last)
<ipython-input-72-1e30353c3485> in <module>()
----> 1 r = urllib.request.urlopen("http://xmh57jrzrnw6insl.onion:80")
      2 r.read()

C:\Users\yella\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    160     else:
    161         opener = _opener
--> 162     return opener.open(url, data, timeout)
    163 
    164 def install_opener(opener):

C:\Users\yella\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
    463             req = meth(req)
    464 
--> 465         response = self._open(req, data)
    466 
    467         # post-process response

C:\Users\yella\Anaconda3\lib\urllib\request.py in _open(self, req, data)
    481         protocol = req.type
    482         result = self._call_chain(self.handle_open, protocol, protocol +
--> 483                                   '_open', req)
    484         if result:
    485             return result

C:\Users\yella\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
    441         for handler in handlers:
    442             func = getattr(handler, meth_name)
--> 443             result = func(*args)
    444             if result is not None:
    445                 return result

C:\Users\yella\Anaconda3\lib\urllib\request.py in http_open(self, req)
   1266 
   1267     def http_open(self, req):
-> 1268         return self.do_open(http.client.HTTPConnection, req)
   1269 
   1270     http_request = AbstractHTTPHandler.do_request_

C:\Users\yella\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
   1240                 h.request(req.get_method(), req.selector, req.data, headers)
   1241             except OSError as err: # timeout error
-> 1242                 raise URLError(err)
   1243             r = h.getresponse()
   1244         except:

URLError: <urlopen error [Errno 11001] getaddrinfo failed>

1 个答案:

答案 0 :(得分:2)

我在Linux上,但你提供的代码对我不起作用。从它的外观来看,DNS解析不会发生在Tor上(基于错误11001 WSAHOST_NOT_FOUND)。我有点怀疑它实际上是因为10061(连接被拒绝)错误而使用Tor。

无论如何,我能够解决这个问题:

import urllib2
import socks
from sockshandler import SocksiPyHandler

opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 9050, True))
print opener.open("http://xmh57jrzrnw6insl.onion").read()

PySocks在docs中说:

  

请注意,monkeypatching可能不适用于所有标准模块或   所有第三方模块,一般不推荐。   Monkeypatching通常是Python中的反模式。

猴子修补是使用socket.socket = socks.socksocket

如果可能,请将Requests与代理人的socks5h://协议处理程序一起使用:

import requests
import json

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

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

print(data)