如何在 Python 2.7 中设置 HTTP 代理? [英] How to set an HTTP proxy in Python 2.7?

查看:78
本文介绍了如何在 Python 2.7 中设置 HTTP 代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个安装 pip 的脚本:get-pip.py 并且由于我的网络位于 HTTP 代理后面而导致连接超时.有什么方法可以在我的 Python 2.7 安装中配置 HTTP 代理,以便能够安装我想要安装的东西?

I am trying to run a script that installs pip: get-pip.py and am getting a connection timeout due to my network being behind an HTTP proxy. Is there some way I could configure an HTTP proxy in my Python 2.7 installation to be able to install what I am trying to install?

注意:我使用的是 Windows.以下是我收到的错误:

Note: I am using Windows. Below is the error I am getting:

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

推荐答案

看起来 get-pip.py 已更新为使用环境变量 http_proxy 和 <代码>https_proxy.

It looks like get-pip.py has been updated to use the environment variables http_proxy and https_proxy.

视窗:

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

Linux/OS X:

Linux/OS X:

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

但是,如果这仍然对您不起作用,您始终可以使用 setuptools' 通过代理安装 pipcode>easy_install 通过设置相同的环境变量.

However if this still doesn't work for you, you can always install pip through a proxy using setuptools' easy_install by setting the same environment variables.

视窗:

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

Linux/OS X:

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 手册页:

--代理
让 pip 使用代理服务器访问站点.这可以指定使用user:password@proxy.server:port"表示法.如果密码被排除在外,pip 会要求它.

--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.

这篇关于如何在 Python 2.7 中设置 HTTP 代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆