【问题标题】:How to use requests library without system-configured proxies如何在没有系统配置代理的情况下使用请求库
【发布时间】:2014-08-14 08:05:21
【问题描述】:

如果我向proxies 参数提供None 或空的dictrequests 将自动回退到通过urllib.request.getproxies() (Python 3) / @987654326 获得的为操作系统配置的代理@。

import requests
r = requests.get('http://google.com', proxies = {}) # or = None...
print(r.text)

指定proxies = { 'http': False } 甚至会导致requests 因任何奇怪的原因而完全挂起。

那么我如何指导requests 直接执行HTTP 请求,而不需要任何代理?

【问题讨论】:

    标签: python proxy python-requests


    【解决方案1】:

    事实证明,您必须为要使用直接连接的协议使用空字符串:

    r = requests.get('http://google.com', proxies = { 'http': '', ... })
    

    很奇怪,但这就是生活。

    【讨论】:

    • 奇怪的是,如果我将它设置为 Session 对象,这似乎对我不起作用,尽管它适用于 requests.get() 更新:使用 os.environ['HTTPS_PROXY']='' 有效
    • (这是因为我在 Windows 用户环境变量中明确设置了 HTTPS_PROXY 以使 pip 工作)
    猜你喜欢
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2016-07-23
    • 2011-12-06
    • 2020-04-14
    • 1970-01-01
    • 2011-03-03
    • 2012-02-26
    相关资源
    最近更新 更多