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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM