5

Unable to use Tor with Python Requests

import requests
proxies = {
    'http': 'socks5://localhost:9050',
    'https': 'socks5://localhost:9050'
}
url = 'http://httpbin.org/ip'
print(requests.get(url, proxies=proxies).text)

I have tried a multitude of solutions, none of which worked for me. I am trying to make simple requests with Python through Tor. Thanks in advance.

Error:

requests.exceptions.ConnectionError: SOCKSHTTPSConnectionPool(host='canihazip.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('< urllib3.contrib.socks.SOCKSHTTPSConnection object at 0x031B77F0>: Failed to esta blish a new connection: [Errno 10061] No connection could be made because the ta rget machine actively refused it',))

Vaxe
  • 51
  • 1
  • 6

2 Answers2

2

First make sure you pip3 install requests[socks], or if using zsh, pip3 install "requests[socks]"

Then do this:

import requests
session = requests.session()
proxies = {
    'http': 'socks5h://localhost:9050',
    'https': 'socks5h://localhost:9050'
}
session.get(url, proxies=proxies)

Note The h in socks5h://

Also, you will have to have tor running on your computer (not the browser). You can install tor using homebrew by running brew install tor.

You an start a tor instance by simply running tor in terminal.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
  • I see a couple of problems, I'm on windows, and the "h" did not change anything for me.. – Vaxe Mar 29 '19 at 21:31
  • I cannot find where to download homebrew, but I do have Tor browser running, yes. – Vaxe Mar 29 '19 at 22:05
  • @Vaxe Sorry I misread the homebrew docs. It does **not** work on windows. Also, I couldn't figure out how to run tor on windows, and you definitely are not the first person to ask. If you figure it out you should let us know! – Lord Elrond Apr 03 '19 at 13:57
  • 1
    I had a related problem, socks was working on windows without `h` but not for my raspberry, adding `h` fixed it. Thanks – Max Feb 07 '20 at 09:21
0

drew010's comment should be the answer

Tor browser listens on port 9150, not 9050 (used by tor expert bundle / tor daemon)

wallfell00
  • 58
  • 1
  • 3