如何在Python2.7中设置HTTP代理?

2024-04-24 06:16:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行一个安装pip:get-pip.py的脚本,由于我的网络位于一个HTTP代理后面,因此连接超时。我是否可以在Python 2.7安装中配置HTTP代理来安装我要安装的内容?

注意:我用的是Windows。下面是我得到的错误:

C:\SetupFiles>python get-pip.py
Downloading/unpacking pip
  Cannot fetch index base URL http://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement pip
No distributions at all found for pip

Tags: pippy网络脚本http代理内容get
3条回答

看起来get-pip.py已经更新为使用环境变量http_proxyhttps_proxy

窗口:

set http_proxy=http://proxy.myproxy.com
set https_proxy=https://proxy.myproxy.com
python get-pip.py

Linux/OS X操作系统:

export http_proxy=http://proxy.myproxy.com
export https_proxy=https://proxy.myproxy.com
sudo -E python get-pip.py

但是,如果这仍然不适用于您,您可以通过使用setuptools'easy_install的代理安装pip,方法是设置相同的环境变量。

窗口:

set http_proxy=http://proxy.myproxy.com
set https_proxy=https://proxy.myproxy.com
easy_install pip

Linux/OS X操作系统:

export http_proxy=http://proxy.myproxy.com
export https_proxy=https://proxy.myproxy.com
sudo -E easy_install pip

安装后,请使用:

pip install --proxy="user:password@server:port" packagename

pip man page

--proxy
Have pip use a proxy server to access sites. This can be specified using "user:password@proxy.server:port" notation. If the password is left out, pip will ask for it.

在我的网络上,仅仅设置http_代理对我不起作用。以下几点是相关的。

1执行sudo时,不会保留为用户设置http_代理-若要保留,请执行以下操作:

sudo -E yourcommand

我通过首先安装cntlm本地代理使安装工作正常。这里的说明很简洁:http://www.leg.uct.ac.za/howtos/use-isa-proxies

你可以输入你的域名而不是学生号

2要使用cntlm本地代理,请执行:

pip install --proxy localhost:3128 pygments

您可以使用easy_install安装pip(或任何其他包),几乎如第一个答案中所述。不过,您也需要一个HTTPS代理。完整的命令序列是:

set http_proxy=http://proxy.myproxy.com
set https_proxy=http://proxy.myproxy.com
easy_install pip

您可能还需要向代理添加端口,例如http{s}_proxy=http://proxy.myproxy.com:8080

相关问题 更多 >