为什么我总是得到HTTP 407:需要代理身份验证?

时间:2014-09-19 17:49:52

标签: python proxy urllib python-3.4

我使用以下内容 -

import urllib.request as req

proxy = req.ProxyHandler({'http': r'http://USER:PASS@PROXY:PORT'})
auth = req.HTTPBasicAuthHandler()
opener = req.build_opener(proxy, auth, req.HTTPHandler)
req.install_opener(opener)
conn = req.urlopen('http://google.com')
return_str = conn.read()

这是我的追溯 -

Traceback (most recent call last):
  File ".\proxy.py", line 8, in <module>
    conn = req.urlopen('http://google.com')
  File "D:\Python34\lib\urllib\request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "D:\Python34\lib\urllib\request.py", line 461, in open
    response = meth(req, response)
  File "D:\Python34\lib\urllib\request.py", line 571, in http_response
    'http', request, response, code, msg, hdrs)
  File "D:\Python34\lib\urllib\request.py", line 499, in error
    return self._call_chain(*args)
  File "D:\Python34\lib\urllib\request.py", line 433, in _call_chain
    result = func(*args)
  File "D:\Python34\lib\urllib\request.py", line 579, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 407: Proxy Authentication Required

尽管这应该是多么简单,但我总是得到407: Proxy Authentication Required。我已经检查了很多关于这个问题的问题,但是找不到有效的答案。似乎urllib似乎没有通过我的凭据。我可以输入一个伪造的密码,并且它没有返回说我的凭证无效。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

最有可能的是,您的代理不接受网址中嵌入的用户名和密码。并且ProxyHandler不会自动将它们从URL中删除并使用它们进行身份验证。因此,您需要查看代理所需的身份验证类型,并使用ProxyBasicAuthHandlerProxyDigestAuthHandler等。

如果查看Examples,第8个会显示如何执行此操作:

proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')