【问题标题】:Proxy connection with Python与 Python 的代理连接
【发布时间】:2013-05-23 00:51:57
【问题描述】:

我一直在尝试从 python 连接到 URL。我试过了: urllib2、urllib3 和请求。这是我在所有情况下都遇到的同一个问题。一旦我得到答案,我想他们三个都可以正常工作。

问题是通过代理连接。我已经输入了我们的代理信息,但没有得到任何快乐。我收到 407 代码和错误消息,例如: HTTP 错误 407:需要代理身份验证(Forefront TMG 需要授权才能完成请求。对 Web 代理筛选器的访问被拒绝。)

但是,我可以使用另一个通过代理的其他应用程序进行连接,例如 git。当我运行git config --get htpp.proxy 时,它返回的值和格式与我在 Python 中输入的相同,即

http://username:password@proxy:8080

请求中的代码示例是

import requests
proxy = {"http": "http://username:password@proxy:8080"}
url = 'http://example.org'
r = requests.get(url,  proxies=proxy)
print r.status_code

感谢您的宝贵时间

【问题讨论】:

    标签: python proxy urllib3


    【解决方案1】:

    在requests模块中,进行代理认证如图:

    import requests
    proxies = {'http':'http://x.x.x.x', 'https':'https://x.x.x.x'}
    auth = requests.auth.HTTPProxyAuth('username', 'password')
    r = requests.get('http://www.example.com', proxies = proxies, auth = auth)
    print r.status_code, r.reason
    

    【讨论】:

      【解决方案2】:

      我通过安装 CNTLM 解决了我的问题。 一旦设置和配置好,我设置 HTTP_PROXY 等。

      【讨论】:

        【解决方案3】:

        您可以在 python 中使用HTTP_PROXY 来连接到您的代理服务器。您可以在提供的此链接上找到更多详细信息。

        http://www.wkoorts.com/wkblog/2008/10/27/python-proxy-client-connections-requiring-authentication-using-urllib2-proxyhandler/

        上面的链接显示了使用urllib2 的示例。实际上,我已经使用它一段时间来同时连接到两台服务器。希望对你有帮助

        【讨论】:

        • 感谢您的快速回复。不幸的是,链接上的代码对我不起作用。它给出了“urllib2.HTTPError:HTTP 错误 407:需要代理身份验证(Forefront TMG 需要授权才能完成请求。访问 Web 代理过滤器被拒绝。)”的错误。
        【解决方案4】:
        import requests
        proxies = {'https':'https://username:password@1.4.1.2:8080'}
        r = requests.get('http://www.example.com', proxies = proxies)
        print r.status_code, r.reason
        

        一个有效的解决方案!在我的服务器上测试过

        【讨论】:

          猜你喜欢
          • 2018-06-19
          • 2013-04-27
          • 1970-01-01
          • 2019-01-03
          • 1970-01-01
          • 2020-02-11
          • 1970-01-01
          • 2015-08-12
          • 2017-11-07
          相关资源
          最近更新 更多