Python 请求:如何禁用/绕过代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28521535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
requests: how to disable / bypass proxy
提问by t777
I am getting an url with:
我得到一个网址:
r = requests.get("http://myserver.com")
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
.
正如我在“myserver.com”的“access.log”中看到的那样,使用了客户端的系统代理。但我想完全禁止使用代理requests
。
采纳答案by Lukas Graf
The only way I'm currently aware of for disabling proxies entirelyis the following:
我目前知道的完全禁用代理的唯一方法如下:
- Create a session
- Set
session.trust_env
toFalse
- Create your request using that session
- 创建会话
- 设置
session.trust_env
为False
- 使用该会话创建您的请求
import requests
session = requests.Session()
session.trust_env = False
response = session.get('http://www.stackoverflow.com')
This is based on this comment by Lukasaand the (limited) documentation for requests.Session.trust_env
.
这是基于Lukasa 的此评论和requests.Session.trust_env
.
Note:Setting trust_env
to False
also ignores the following:
注意:设置trust_env
为False
也会忽略以下内容:
- Authentication information from
.netrc
(code) - CA bundles defined in
REQUESTS_CA_BUNDLE
orCURL_CA_BUNDLE
(code)
If however you only want to disable proxies for a particular domain (like localhost
), you can use the NO_PROXY
environment variable:
但是,如果您只想禁用特定域的代理(如localhost
),则可以使用NO_PROXY
环境变量:
import os
import requests
os.environ['NO_PROXY'] = 'stackoverflow.com'
response = requests.get('http://www.stackoverflow.com')
回答by KostasT
requests library respects environment variables. http://docs.python-requests.org/en/latest/user/advanced/#proxies
请求库尊重环境变量。 http://docs.python-requests.org/en/latest/user/advanced/#proxies
So try deleting environment variables HTTP_PROXY and HTTPS_PROXY.
所以尝试删除环境变量 HTTP_PROXY 和 HTTPS_PROXY。
import os
for k in list(os.environ.keys()):
if k.lower().endswith('_proxy'):
del os.environ[k]
回答by jtpereyda
You can choose proxies for each request. From the docs:
您可以为每个请求选择代理。从文档:
import requests
proxies = {
"http": "http://10.10.1.10:3128",
"https": "http://10.10.1.10:1080",
}
requests.get("http://example.org", proxies=proxies)
So to disable the proxy, just set each one to None:
所以要禁用代理,只需将每个设置为无:
import requests
proxies = {
"http": None,
"https": None,
}
requests.get("http://example.org", proxies=proxies)
回答by Pierz
The way to stop requests/urllib from proxying any requests is to set the the no_proxy
(or NO_PROXY
) environment variable to *
e.g. in bash:
阻止 requests/urllib 代理任何请求的方法是将no_proxy
(或NO_PROXY
)环境变量设置为*
例如在 bash 中:
export no_proxy='*'
Or from Python:
或者来自 Python:
import os
os.environ['no_proxy'] = '*'
To understand why this works is because the urllib.request.getproxiesfunction first checks for any proxies set in the environment variables (e.g. http_proxy etc) or if none are set then it will check for system configured proxies using platform specific calls (e.g. On MacOS it will check using the system scutil/configd interfaces, and on Windows it will check the Registry).
要理解为什么这样做是因为urllib.request.getproxies函数首先检查环境变量中设置的任何代理(例如 http_proxy 等),或者如果没有设置,则它将使用特定于平台的调用检查系统配置的代理(例如在 MacOS 上)它将使用系统 scutil/configd 接口进行检查,在 Windows 上它将检查注册表)。
Then when urllib attempts to use any proxies the proxyHandler
function it will check for the presence and setting of the no_proxy
environment variable - which can either be set to specific hostnames as mentioned above or it can be set the special *
value whereby all hosts bypass the proxy.
然后,当 urllib 尝试使用任何代理时,该proxyHandler
函数将检查no_proxy
环境变量的存在和设置- 可以将其设置为如上所述的特定主机名,也可以设置特殊*
值,从而所有主机都绕过代理。