Saturday, February 18, 2017

Get pip of python install to work behind proxy

OK, here is a simple task, use pip to install packages...
Other than if you are behind a proxy that uses https.

What happens is even though you issue the proper command line like below:
# pip install virtualenv --proxy http://user:pass@proxy:8080
you still get an error message like this:
.....
CertificateError: hostname 'proxy' doesn't match either of 'www.python.org', 'docs.python.org', 'bugs.python.org'.....

You tried to use HTTP_PROXY environment variable as well, still this Cert error??!!

Hmmmm.... what is going on?

It is because the ssl.py actually checks the hostname of the certificate subjectAltName, which would be python.org hostnames.  However, the 'hostname' it connected to is actually your proxy server, not the python servers.

OK, here is what you need to do - update the /usr/lib64/python2.7/ssl.py file
# vi /usr/lib64/pyton2.7/ssl.py

look for "if len(dnsnames) > 1:"

immediately before this line, you should insert below block:
value = 'proxy'
if _dnsname_match(value, hostname):
    return
dnsnames.append(value)

This is tested on python 2.7 behind a firewall.

Not all proxy servers will cause this problem, but if it does, here is the quick fix.