【问题标题】:python requests module proxy handler not workingpython请求模块代理处理程序不起作用
【发布时间】:2014-08-07 14:23:19
【问题描述】:

我正在使用 python 请求模块进行 HTTP 通信。我在进行任何通信之前使用了代理。

import requests

proxy = {'http': 'xxx.xxx.xxx.xxx:port'}

                   OR

proxy = {'http': 'http://xxx.xxx.xxx.xxx:port'}

                  OR

proxy = {'http://xxx.xxx.xxx.xxx:port'}


requests.get(url, proxies = proxy)

我正在使用上面的代码将代理添加到请求对象。但它似乎代理不起作用。请求模块正在获取我的网络 IP 并触发请求。

请求模块是否有任何错误或问题或任何其他已知问题,或者我缺少什么。

【问题讨论】:

标签: python proxy python-requests


【解决方案1】:

试试这个:

proxy = {'http': 'http://xxx.xxx.xxx.xxx:port'}

我猜你只是错过了代理字典值中的http://。 检查:http://docs.python-requests.org/en/latest/user/advanced/#proxies

【讨论】:

  • 代理是否需要用户名/密码?
  • No proxy 没有任何用户名。我想我有问题。 python中的请求不支持https。它仅支持 http 代理。如果您尝试使用 https,它将不使用代理,而是使用您的本地 IP 地址进行请求。
  • 什么? requests 肯定支持 https。
  • 我使用 proxy = {'https': 'xxx.xxx.xxx.xxx:port'} 进行了尝试,但它在内部调用我的原始 IP 时不起作用。如果我将其修改为 proxy = {'http': 'xxx.xxx.xxx.xxx:port'} 那么它开始为我工作。
【解决方案2】:

Documentation 说:

如果您需要使用代理,您可以配置单个请求 任何请求方法的代理参数:

import requests
proxies = {"http": "http://10.10.1.10:3128"}
requests.get("http://example.org", proxies=proxies)

这里代理["http"] = "http://xxx.xxx.xxx.xxx:port"。看来你缺少 http://

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多