Sergey Romanov
Sergey Romanov

Reputation: 11

Python. Requests lib working via proxy(socks5 TOR) - error

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'))

Here's code of my Loader class. Loader is responsible for uploading and making requests to resource over TOR. I configured proxy, but it still throws me error. I disabled VPN, Windows firewall and everything what I could.

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}

Code which call Loader class:

http = Loader(Config())
page = http.robotCheck()

Upvotes: 1

Views: 4399

Answers (2)

drew010
drew010

Reputation: 69967

Tor is a SOCKS proxy so the proxy configuration needs to be slightly different.

Change the following lines:

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

To:

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

Port 9051 is the Tor Controller port. For both HTTP and HTTPS SOCKS connections over Tor, use port 9050 (the default SOCKS port).

The socks5h scheme is necessary to have DNS names resolved over Tor instead of by the client. This privatizes DNS lookups and is necessary to be able to resolve .onion addresses.

EDIT: I was able to make a SOCKS request for a .onion address using the following example:

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/')

Make sure you have the most up-to-date requests libraries with pip3 install -U requests[socks]'

Upvotes: 1

nilashan
nilashan

Reputation: 690

Wrap like this to get the exception only.

 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)

Because of the following reasons you may get those errors:

  • your server may refuse your connection (you're sending too many
    requests from same ip address in short period of time)
  • Check your proxy settings

For your reference : https://github.com/requests/requests/issues/1198

Upvotes: 0

Related Questions