蟒蛇。请求lib通过代理工作(socks5 TOR)-错误

时间:2018-07-10 07:43:10

标签: python proxy python-requests tor socks

 {'INFO': {'submit': {'is_test': 'False', 'username': 'Mary'}, 'env': {'sys.platform': 'linux2', 'os.name': 'ubuntu'}}}

这是我的Loader类的代码。加载程序负责通过TOR上传资源并向资源提出请求。我配置了代理,但是仍然抛出错误。 我禁用了VPN,Windows防火墙以及我所能做的一切。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 34, in <module>
    page = http.robotCheck()
  File "C:\Users\user001\Desktop\automation\Loader.py", line 21, in robotCheck
    request = requests.get('http://*redacted*.onion/login')
  File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\adapters.py", line 513, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='*redacted*.onion', port=80): Max retries exceeded with url: /login (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0348E230>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

调用Loader类的代码:

import socket
import requests

class Loader:
    url = "http://*redacted*.onion/login"
    user = "username"
    password = 'password'
    manageUrl = ''

    def __init__(self, config):
        self.config = config

        self.restartSession()

    def restartSession(self):
        self.userSession = requests.session()
        self.userSession.proxies['http'] = 'http://127.0.0.1:9050'
        self.userSession.proxies['https'] = 'https://127.0.0.1:9051'

    def robotCheck(self):
        request = requests.get('http://*redacted*.onion/login')
        print(request)
        #self.session.post(self.robotCheckUrl, data=checkResult)

    def authorization(self):
        self.session.get(self.url)
        authPage = self.session.post(self.url, data = self.getAuthData())


    def getAuthData(self):
        return {'login' : self.user, 'password' : self.password}

2 个答案:

答案 0 :(得分:0)

像这样包装以仅获取异常。

 def robotCheck(self):
    try:

        request = requests.get('http://hydraruzxpnew4af.onion/login')
        print(request)
    except requests.exceptions.RequestException as e:
        print('exception caught', e)
    #self.session.post(self.robotCheckUrl, data=checkResult)

由于以下原因,您可能会得到这些错误:

  • 您的服务器可能拒绝您的连接(您发送的邮件过多
    短时间内来自同一IP地址的请求)
  • 检查您的代理设置

供参考:https://github.com/requests/requests/issues/1198

答案 1 :(得分:0)

Tor是SOCKS代理,因此代理配置需要稍有不同。

更改以下几行:

    self.userSession.proxies['http'] = 'http://127.0.0.1:9050'
    self.userSession.proxies['https'] = 'https://127.0.0.1:9051'

收件人:

    self.userSession.proxies['http'] = 'socks5h://127.0.0.1:9050'
    self.userSession.proxies['https'] = 'socks5h://127.0.0.1:9050'

端口9051是Tor控制器端口。对于通过Tor进行的HTTP和HTTPS SOCKS连接,请使用端口9050(默认的SOCKS端口)。

必须使用socks5h方案,以通过Tor而不是由客户端解析DNS名称。这将DNS查找私有化,这对于解析.onion地址是必需的。

编辑:我可以使用以下示例向.onion地址发出SOCKS请求:

import socket
import requests

s = requests.session()
s.proxies['http'] = 'socks5h://127.0.0.1:9050'
s.proxies['https'] = 'socks5h://127.0.0.1:9050'

print(s.proxies)

r = s.get('http://***site***.onion/')

确保您使用pip3 install -U requests[socks]'

拥有最新的请求库