Python。通过代理(socks5 TOR)工作的请求库 - 错误
Posted
技术标签:
【中文标题】Python。通过代理(socks5 TOR)工作的请求库 - 错误【英文标题】:Python. Requests lib working via proxy(socks5 TOR) - error 【发布时间】:2018-07-10 07:43:09 【问题描述】:During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 34, in <module>
page = http.robotCheck()
File "C:\Users\user001\Desktop\automation\Loader.py", line 21, in robotCheck
request = requests.get('http://*redacted*.onion/login')
File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "C:\Users\user001\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\adapters.py", line 513, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='*redacted*.onion', port=80): Max retries exceeded with url: /login (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x0348E230>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
这是我的 Loader 类的代码。 Loader 负责通过 TOR 上传资源并向资源发出请求。我配置了代理,但它仍然会引发错误。 我禁用了 ***、Windows 防火墙和我能做的一切。
import socket
import requests
class Loader:
url = "http://*redacted*.onion/login"
user = "username"
password = 'password'
manageUrl = ''
def __init__(self, config):
self.config = config
self.restartSession()
def restartSession(self):
self.userSession = requests.session()
self.userSession.proxies['http'] = 'http://127.0.0.1:9050'
self.userSession.proxies['https'] = 'https://127.0.0.1:9051'
def robotCheck(self):
request = requests.get('http://*redacted*.onion/login')
print(request)
#self.session.post(self.robotCheckUrl, data=checkResult)
def authorization(self):
self.session.get(self.url)
authPage = self.session.post(self.url, data = self.getAuthData())
def getAuthData(self):
return 'login' : self.user, 'password' : self.password
调用Loader类的代码:
http = Loader(Config())
page = http.robotCheck()
【问题讨论】:
【参考方案1】:Tor 是 SOCKS 代理,因此代理配置需要稍有不同。
更改以下行:
self.userSession.proxies['http'] = 'http://127.0.0.1:9050'
self.userSession.proxies['https'] = 'https://127.0.0.1:9051'
收件人:
self.userSession.proxies['http'] = 'socks5h://127.0.0.1:9050'
self.userSession.proxies['https'] = 'socks5h://127.0.0.1:9050'
端口 9051 是 Tor 控制器端口。对于通过 Tor 的 HTTP 和 HTTPS SOCKS 连接,请使用端口 9050(默认 SOCKS 端口)。
socks5h
方案是通过 Tor 而不是客户端解析 DNS 名称所必需的。这会将 DNS 查找私有化,并且是解析.onion
地址所必需的。
编辑:我能够使用以下示例向.onion
地址发出 SOCKS 请求:
import socket
import requests
s = requests.session()
s.proxies['http'] = 'socks5h://127.0.0.1:9050'
s.proxies['https'] = 'socks5h://127.0.0.1:9050'
print(s.proxies)
r = s.get('http://***site***.onion/')
使用pip3 install -U requests[socks]'
确保您拥有最新的请求库
【讨论】:
同一个 实际上它抛出“[Errno 11001] getaddrinfo failed”,但主机可以通过tor访问,我现在检查了它 @SergeyRomanov 见编辑。它应该工作。 getaddrinfo 失败意味着它无法解析.onion
地址,这似乎没有使用 Tor 来解析名称。【参考方案2】:
这样包装以仅获取异常。
def robotCheck(self):
try:
request = requests.get('http://hydraruzxpnew4af.onion/login')
print(request)
except requests.exceptions.RequestException as e:
print('exception caught', e)
#self.session.post(self.robotCheckUrl, data=checkResult)
由于以下原因,您可能会收到这些错误:
您的服务器可能会拒绝您的连接(您发送的太多 短时间内来自同一IP地址的请求) 检查您的代理设置供您参考:https://github.com/requests/requests/issues/1198
【讨论】:
结果如下:$ python main.py 异常被捕获 HTTPConnectionPool(host='hydraruzxpnew4af.onion', port=80): url: /login 超过最大重试次数(由 NewConnectionError('以上是关于Python。通过代理(socks5 TOR)工作的请求库 - 错误的主要内容,如果未能解决你的问题,请参考以下文章