请求:如何禁用/绕过代理
我正在使用以下网址:
r = requests.get("http://myserver.com")
正如我在“ myserver.com”的“ access.log”中看到的那样,使用了客户端的系统代理。但是我想完全禁止使用代理requests
。
-
我目前了解的 完全 禁用代理的唯一方法是:
- 建立会议
- 设置
session.trust_env
于False
-
使用该会话创建您的请求
import requests
session = requests.Session()
session.trust_env = Falseresponse = session.get('http://www.codingdict.com’)
这是基于Lukasa的评论和的(受限)文档而得出的
requests.Session.trust_env
。注意: 设置
trust_env
为False
也会忽略以下内容:
但是,如果您只想禁用特定域的代理(如
localhost
),则可以使用NO_PROXY
环境变量:import os import requests os.environ['NO_PROXY'] = 'stackoverflow.com' response = requests.get('http://www.stackoverflow.com')