如何告诉 Python 像 R 的 internet2 选项一样自动使用 Windows XP 中的代理设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17247981/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:53:04  来源:igfitidea点击:

How to tell Python to automatically use the proxy setting in Windows XP like R's internet2 option?

pythonproxy

提问by xiaodai

I am not super technical person. But I know that in Windows if I install R using the internet2 option then I can download whatever package I want in it.

我不是技术超强的人。但我知道在 Windows 中,如果我使用 internet2 选项安装 R,那么我可以下载我想要的任何包。

I install Python and everytime I try to download a package or install a package (e.g. using easy_install) it fails.

我安装了 Python,每次我尝试下载一个包或安装一个包(例如使用 easy_install)时,它都会失败。

What can I do to make Python automatically detect my proxy settings and just install the packages?

我该怎么做才能让 Python 自动检测我的代理设置并安装软件包?

回答by falsetru

Set up environment variable http_proxy/ https_proxyto http://your-proxy-server-address:proxy-port

设置环境变量http_proxy/https_proxyhttp://your-proxy-server-address:proxy-port

The urlopen() function works transparently with proxies which do not require authentication. In a Unix or Windows environment, set the http_proxy, or ftp_proxy environment variables to a URL that identifies the proxy server before starting the Python interpreter. For example (the '%' is the command prompt):

% http_proxy="http://www.someproxy.com:3128"
% export http_proxy
% python
...

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.

urlopen() 函数与不需要身份验证的代理透明地工作。在 Unix 或 Windows 环境中,在启动 Python 解释器之前,将 http_proxy 或 ftp_proxy 环境变量设置为标识代理服务器的 URL。例如('%' 是命令提示符):

% http_proxy="http://www.someproxy.com:3128"
% export http_proxy
% python
...

no_proxy 环境变量可用于指定不应通过代理访问的主机;如果设置,它应该是一个以逗号分隔的主机名后缀列表,可选择附加 :port,例如cern.ch,ncsa.uiuc.edu,some.host:8080。

回答by Andres

Or use the HTTP_PROXY / HTTPS_PROXY setting instead.

或者改用 HTTP_PROXY / HTTPS_PROXY 设置。