6

I am complete newbie to using requests and tor.

The code I am using:

def get_tor_session():
    session = requests.session()
    # Tor uses the 9050 port as the default socks port
    session.proxies = {'http':  'socks5://127.0.0.1:9050',
                   'https': 'socks5://127.0.0.1:9050'}
    return session

session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)

I keep getting the following error:

raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: SOCKSHTTPConnectionPool(host='httpbin.org', port=80): Max retries exceeded with url: /ip (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it',))

I looked at various answers but wasn't able to pin point the problem.

Sid
  • 3,749
  • 7
  • 29
  • 62
  • 1
    Make sure tor is running on localhost, and if you still can't connect try 9150 – t.m.adam Feb 17 '18 at 13:20
  • @t.m.adam I tried changing the port. Still getting an error(error given in a comment to jamescampbell's answer below). Tor is definitely running as chromedriver is using it to get webscrape. – Sid Feb 19 '18 at 05:48
  • Can't you just run your Python script with `torify`? – Boris Verkhovskiy Dec 24 '20 at 18:42

3 Answers3

4

Here is code that works for me:

import requests
torport = 9050
proxies = {
    'http': "socks5h://localhost:{}".format(torport),
    'https': "socks5h://localhost:{}".format(torport)
}

print(requests.get('http://icanhazip.com', proxies=proxies).content)

Check to make sure Tor is running on your machine and that the firewall on Windows / Defender etc. are not blocking the port you are trying to access.

james-see
  • 12,210
  • 6
  • 40
  • 47
  • Hi, thanks for the reply. Tor is running, I am using chromedriver with another program with tor and it works properly so I a assume the port is not being blocked. Now I am getting the following error when I run the code above: >"Unable to determine SOCKS version from %s" % proxy_url" Can't figure out what I am doing wrong here. – Sid Feb 19 '18 at 05:33
  • did you make sure you have pysocks installed? I would it via `sudo pip3 install requests[socks]` to ensure you install the version that works monkey-patched correctly with requests. – james-see Feb 24 '18 at 21:51
  • @Sid great to hear. – james-see Feb 28 '18 at 13:09
1

you can change code like this :

def __init__(self):
    self.header = {'Content-Type': 'application/x-www-form-urlencoded'}

# ***** proxy *****#
def renew_connection(self):
    with db.Controller.from_port(port=9051) as controller:
        controller.authenticate(password='my_password')
        controller.signal(Signal.NEWNYM)
        controller.close()

def request_tor(self, url, headers, post_fields):
    self.renew_connection()
    session = requests.session()
    session.proxies = {}
    session.proxies['http'] = 'socks5h://localhost:9050'
    r = session.post(url, headers=headers, data=post_fields)
Lashgari
  • 41
  • 11
0

I had the same problem, and the below solution fixed it.

SOCKSHTTPConnectionPool(host='httpbin.org', port=80)

according to this line, your computer's listening port is configured with port 80 which needs to be changed to the SOCKSPort you configured in torrc file, mine SOCKSPort=9050 and by default, the CONTROL PORT value is set to 9051. How to configure a tor proxy on windows? This link explains how to configure tor. make sure tor browser is running in the background. Hope this info helps.