Tag Archives: Requests Configure tor proxy Error

[Solved] Requests Configure tor proxy Error: ‘Failed to establish a new connection: [Errno -2] Name or service not known’

Requests Configure tor proxy Error: ‘Failed to establish a new connection: [Errno -2] Name or service not known’
socks5 Change to socks5h

Using the schema socks5 results in DNS resolution happening on the client and not on the proxy. This corresponds to curl, which uses a scheme to decide whether to do DNS resolution on the client or proxy. If you want to allow domains on the proxy, use socks5h as schema.

socks5: DNS resolution without proxy
socks5h: DNS resolution is also proxied
Solution:

import requests

session = requests.session()
session.proxies = {'http': 'socks5h://127.0.0.1:9150', 'https': 'socks5h://127.0.0.1:9150'}
response = session.get("https://3g2upl4pq6kufc4m.onion/")
print(response)
#<Response [200]>