Search code examples
pythonpython-requeststorsockstorsocks

Unable to make python requests over tor ConnectionRefusedError: [WinError 10061]


I am trying to make requests using python requests over tor, but i get the error "ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it".

Here is the code i am using:

import requests

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

# Make a request through the Tor connection
# IP visible through Tor
session = get_tor_session()
print(session.get("http://httpbin.org/ip").text)
# Above should print an IP different than your public IP

# Following prints your normal public IP
print(requests.get("http://httpbin.org/ip").text)

I have tried disabling the firewall etc but can't seems to understand the problem, any help would be appreciated. I am using python 3.7 windows 10.


Solution

  • Thanks for all the help, yes as already mentioned in the comments, there was something wrong with the proxy.

    I changed:

    session.proxies = {'http':  'socks5://127.0.0.1:9050',
                       'https': 'socks5://127.0.0.1:9050'}
    

    To:

    session.proxies = {'http':  'socks5://127.0.0.1:9150',
                       'https': 'socks5://127.0.0.1:9150'}
    

    90 to 91 in address and it worked !