requests 配置tor代理后提示'Failed to establish a new connection: [Errno -2] Name or service not known'

requests 配置代理后提示'Failed to establish a new connection: [Errno -2] Name or service not known'

socks5换成socks5h

https://translate.google.com/website?sl=ru&tl=en&nui=1&prev=search&u=http://docs.python-requests.org/en/master/user/advanced/%23socks

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解析不经过代理
socks5h: DNS解析也经过代理

解决方法:

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]>

参考链接: https://coderoad-ru.translate.goog/47338274/Python-requests-терпит-неудачу-при-попытке-подключиться-к-сайту-onion?_x_tr_sl=ru&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=nui,sc

posted @ 2021-10-12 11:16  twfb  阅读(1616)  评论(0编辑  收藏  举报