我正在尝试打开一个受身份验证保护的网站。我决定使用请求模块,它与基本网站(facebook是我的主要测试网站)一起使用时效果很好。但是,当我尝试在具有“更高级”身份验证的网站(例如,多个防火墙以及受密码保护的网站)上使用它时,出现以下错误:
Traceback (most recent call last):
File "C:\Python27\Pre-SiDashboard\more_test.py", line 15, in <module>
r = requests.get(url, proxies=proxies, auth=('user','pass'))
File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\api.py", line 55, in get
return request('get', url, **kwargs)
File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\sessions.py", line 567, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\adapters.py", line 395, in send
raise ProxyError(e)
ProxyError: ('Cannot connect to proxy.', error(10054, 'An existing connection was forcibly closed by the remote host'))
这是我的代码(隐藏了网站,代理和密码):
import requests
from requests.auth import HTTPBasicAuth
import getpass
proxies = {
"http": "http://a_proxy.blah.blah.org",
"https": "http://a_proxy.blah.blah.org",
}
url = 'https://example.com/here/there/'
user = getpass.getuser()
password = getpass.getpass("password:")
r = requests.get(url, proxies=proxies, auth=('user','pass'))
#print r.text
print r.cookies
print r.status_code
另一个问题告诉我错误是什么,但我不知道如何解决...
感谢您的帮助:)
最佳答案
如果由于身份验证过程而出现问题,则应将用户名和密码传递给auth参数(假设凭据是用户的输入):
r = requests.get(url, proxies=proxies, auth=HTTPProxyAuth(user, password))