【问题标题】:Unable to retrieve the url with request due to proxy error, but with proxy settings set properly. Working in SSO environment由于代理错误,无法通过请求检索 url,但代理设置设置正确。在 SSO 环境中工作
【发布时间】:2019-03-05 14:56:46
【问题描述】:

我支持代理设置,代理设置上的环境变量设置正确。例如,在进行 pip 安装时,环境变量可以正常工作。我使用 getproxies 方法检索代理设置,我检查并返回了正确的 dict。

我正在尝试以下方法: 导入请求 导入urllib

r = requests.get('http://www.nu.nl', proxies=urllib.request.getproxies())

我得到的错误信息:

ProxyError: HTTPSConnectionPool(host='www.nu.nl', port=443): Max retries 
exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', 
OSError('Tunnel connection failed: 407 authenticationrequired',)))

我在这里错过了什么?

【问题讨论】:

    标签: python http proxy python-requests


    【解决方案1】:

    好的,想通了。我的环境变量的问题是未指定用户名和密码,因为我在单点登录环境中工作。因此需要正确设置密码。只是在代理定义中替换我的凭据不起作用,所以我不得不使用 urllib 开启程序来解决我的问题。现在它就像一个魅力。

    import urllib
    
    username = 'userID'  # ex. ID
    password = "password"  # password
    
    targetUrl = "http://www.example.org/"
    
    proxies = {
       'https':  'https://{}:{}@proxyAdress:port'.format(username, password)}
    proxy = urllib.request.ProxyHandler(proxies)
    opener = urllib.request.build_opener(proxy)
    urllib.request.install_opener(opener)
    
    with urllib.request.urlopen(targetUrl) as url:
        text = str(url.read())
    

    【讨论】:

      【解决方案2】:

      HTTP 407 代码表明您需要对代理服务器进行身份验证。
      http 响应头 Proxy-Authenticate 会告诉你需要什么样的身份验证 - 打印出响应头。

      也可能是网址的问题...尝试删除 www 位

      供参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/407

      【讨论】:

      • 如果我点击该链接,我仍然不知道该怎么做。你能解释一下需要做什么吗?谢谢
      • r.headers = Out[6]: {'Date': 'Tue, 05 Mar 2019 14:59:26 GMT', 'Content-Type': 'text/html', 'Cache -Control':'no-cache','Content-Length':'3637','Proxy-Connection':'Keep-Alive','Proxy-Authenticate':'Negotiate,NTLM,Basic realm="McAfee Web Gateway "'}
      • 这是打印出来的意思吗?
      • 是的......顺便说一句,它是否适用于这个网址:https://nu.nl?注意http(s)的使用和www的删除
      • 您可能需要添加您的 Windows 名称和密码,请参阅类似问题:stackoverflow.com/questions/34079/…
      猜你喜欢
      • 2021-07-04
      • 2016-08-10
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多