Search code examples
pythontor

Can't connect to tor website through python


I want to parse an onion website with python, came up with that code:

import requests

proxies = {
    'http': 'socks5://localhost:9150',
    'https': 'socks5://localhost:9150'
}
url = 'http://sblib3fk2gryb46d.onion'

print(requests.get(url, proxies=proxies).text)

Gives me an error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 851, in connect
    negotiate(self, dest_addr, dest_port)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 497, in _negotiate_SOCKS5
    self, CONNECT, dest_addr)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 565, in _SOCKS5_request
    resolved = self._write_SOCKS5_address(dst, writer)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/socks.py", line 621, in _write_SOCKS5_address
    socket.AI_ADDRCONFIG)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/socket.py", line 743, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

Works just fine with clearnet websites. Tor is opened, I use mac os. I've tried a few various options, this one is the only one that is working so far(at least i managed to connect through tor to clearnet)

Maybe someone did smth like that before and can help?


Solution

  • In order to resolve DNS and also be able to connect to .onion addresses, you need to use the socks5h protocol to tell requests to resolve DNS over Tor. The example above is just socks5 which means it is trying to use your regular DNS resolver to resolve the .onion address which will not work.

    To fix, change your proxies to:

    proxies = {
        'http': 'socks5h://localhost:9150',
        'https': 'socks5h://localhost:9150'
    }
    

    Also, for future readers, port 9150 is used by Tor browser bundle. For using the Tor daemon, try port 9050.