gpt4 book ai didi

python - 为什么我总是收到 HTTP 407 : Proxy Authentication Required?

转载 作者:行者123 更新时间:2023-12-01 05:01:52 25 4
gpt4 key购买 nike

我正在使用以下内容 -

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:需要代理身份验证。我已经检查了很多关于这个问题的问题,但找不到有效的答案。看起来好像 urllib 根本没有传递我的凭据。我可以输入伪造的密码,但它不会返回说我的凭据无效。

我错过了什么?

最佳答案

您的代理很可能不接受 URL 中嵌入的用户名和密码。并且 ProxyHandler 不会自动将它们从 URL 中剥离出来并使用它们进行身份验证。因此,您需要查看代理想要什么类型的身份验证并使用 ProxyBasicAuthHandler或 ProxyDigestAuthHandler 等。

如果你看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')

关于python - 为什么我总是收到 HTTP 407 : Proxy Authentication Required?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25939656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com