【问题标题】:Why do I always get HTTP 407: Proxy Authentication Required?为什么我总是收到 HTTP 407:需要代理身份验证?
【发布时间】:2014-09-19 17:49:51
【问题描述】:

我正在使用以下 -

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 根本没有通过我的凭据。我可以输入一个伪造的密码,但它不会返回说我的凭据无效。

我错过了什么?

【问题讨论】:

    标签: python proxy urllib python-3.4


    【解决方案1】:

    您的代理很可能不接受 URL 中嵌入的用户名和密码。并且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')
    

    【讨论】:

    • 你知道吗?我曾尝试将我的realm 设置为None,而我得到的只是AbstractBasicAuthHandler does not support the following scheme: 'NEGOTIATE'
    • @user3238014:嗯,在编写代码之前,您需要弄清楚代理希望您如何进行身份验证。您可以通过反复试验、查看浏览器的配置并理解其含义、查看浏览器和代理之间的流量、查看代理的配置来做到这一点……但不知何故,您必须这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    相关资源
    最近更新 更多