Python http(s)请求不使用代理

结论

import os
os.environ["no_proxy"] = "*"

说明

根据urllib.request.urlopen文档的说法:

In addition, if proxy settings are detected (for example, when a `*_proxy` environment variable like `http_proxy` is set), [`ProxyHandler`](#urllib.request.ProxyHandler) is default installed and makes sure the requests are handled through the proxy.

如果检测到操作系统存在代理设置,默认就会保证网络请求走代理。

根据urllib.request.ProxyHandler文档的说法:

To disable autodetected proxy pass an empty dictionary.

The no_proxy environment variable can be used to specify hosts which shouldn’t be reached via proxy; if set, it should be a comma-separated list of hostname suffixes, optionally with :port appended, for example cern.ch,ncsa.uiuc.edu,some.host:8080.

如果不想要自动走代理,可以设置一个no_proxy的环境变量

Back to Top