Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an HTTP proxy in Python 2.7?

Tags:

python

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?

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 
like image 400
Rolando Avatar asked Jul 30 '12 17:07

Rolando


People also ask

How do I create an HTTP proxy server in Python?

Creating an incoming socket We create a socket serverSocket in the __init__ method of the Server Class. This creates a socket for the incoming connections. We then bind the socket and then wait for the clients to connect.

How do I set a proxy on PIP?

pip can be configured to connect through a proxy server in various ways: using the --proxy command-line option to specify a proxy in the form scheme://[user:passwd@]proxy.server:port. using proxy in a Configuration Files. by setting the standard environment-variables http_proxy , https_proxy and no_proxy .

What is proxy in Python?

Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.


2 Answers

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

Windows:

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 

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.

Windows:

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 

Then once it's installed, use:

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

From the pip man page:

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

like image 111
Ben Burns Avatar answered Oct 08 '22 14:10

Ben Burns


On my network just setting http_proxy didn't work for me. The following points were relevant.

1 Setting http_proxy for your user wont be preserved when you execute sudo - to preserve it, do:

sudo -E yourcommand 

I got my install working by first installing cntlm local proxy. The instructions here is succinct : http://www.leg.uct.ac.za/howtos/use-isa-proxies

Instead of student number, you'd put your domain username

2 To use the cntlm local proxy, exec:

pip install --proxy localhost:3128 pygments 
like image 38
eugenevd Avatar answered Oct 08 '22 12:10

eugenevd