请求:如何禁用/绕过代理 [英] requests: how to disable / bypass proxy
问题描述
我得到一个网址:
r = requests.get("http://myserver.com")
正如我在myserver.com"的access.log"中看到的,使用了客户端的系统代理.但我想完全禁用 requests
使用代理.
As I can see in the 'access.log' of "myserver.com", the client's system proxy is used. But I want to disable using proxies at all with requests
.
推荐答案
我目前知道的禁用代理的唯一方法完全如下:
The only way I'm currently aware of for disabling proxies entirely is the following:
- 创建会话
- 将
session.trust_env
设置为False
- 使用该会话创建您的请求
import requests
session = requests.Session()
session.trust_env = False
response = session.get('http://www.stackoverflow.com')
这是基于 此评论来自 Lukasa 和(有限) requests.Session.trust_env
.
This is based on this comment by Lukasa and the (limited) documentation for requests.Session.trust_env
.
注意:将 trust_env
设置为 False
也会忽略以下内容:
Note: Setting trust_env
to False
also ignores the following:
- Authentication information from
.netrc
(code) - CA bundles defined in
REQUESTS_CA_BUNDLE
orCURL_CA_BUNDLE
(code)
但是,如果您只想禁用特定域的代理(例如 localhost
),您可以使用 NO_PROXY
环境变量:
If however you only want to disable proxies for a particular domain (like localhost
), you can use the NO_PROXY
environment variable:
import os
import requests
os.environ['NO_PROXY'] = 'stackoverflow.com'
response = requests.get('http://www.stackoverflow.com')
这篇关于请求:如何禁用/绕过代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!