Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing Python's easy_install using ez_setup.py from behind a proxy server

Is there a way to install Python's easy_install using ez_setup.py when on a corporate network that uses a proxy server? Currently, I receive a connection timeout:

Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
Traceback (most recent call last):
  File "C:\jsears\python\ez_setup.py", line 278, in <module>
    main(sys.argv[1:])
  File "C:\jsears\python\ez_setup.py", line 210, in main
    egg = download_setuptools(version, delay=0)
  File "C:\jsears\python\ez_setup.py", line 158, in download_setuptools
    src = urllib2.urlopen(url)
  File "C:\jsears\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\jsears\Python27\lib\urllib2.py", line 400, in open
    response = self._open(req, data)
  File "C:\jsears\Python27\lib\urllib2.py", line 418, in _open
    '_open', req)
  File "C:\jsears\Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\jsears\Python27\lib\urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\jsears\Python27\lib\urllib2.py", line 1177, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>
like image 428
jsears Avatar asked Dec 05 '12 22:12

jsears


2 Answers

On Windows 7, with PowerShell, the proxy settings above are ignored, and the tool won't work. But I found the solution.

I modified the routine download_file_powershell by adding

[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials;

inside the scriptlet used to download via the WebClient class. Here is the complete download_file_powershell function now:

def download_file_powershell(url, target):
"""
Download the file at url to target using Powershell (which will validate
trust). Raise an exception if the command cannot complete.
"""
target = os.path.abspath(target)
cmd = [
    'powershell',
    '-Command',
    "[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials;  (new-object System.Net.WebClient).DownloadFile(%(url)r, %(target)r)" % vars(),
]
subprocess.check_call(cmd)
like image 145
valir Avatar answered Sep 28 '22 09:09

valir


If you already have a http_proxy/https_proxy environment variable set you can just tell ez_setup.py to not use PowerShell. PowerShell does not use HTTP_PROXY/HTTPS_PROXY environment variables. Follow the first section in this response.

For people who might not know how to set environment variables, see sections 2+.

Stop ez_setup.py from using PowerShell

Go into the ez_install.py and find the following section:

def has_powershell():
    if platform.system() != 'Windows':
        return False
    cmd = ['powershell', '-Command', 'echo test']
    devnull = open(os.path.devnull, 'wb')
    try:
        try:
            subprocess.check_call(cmd, stdout=devnull, stderr=devnull)
        except:
            return False
    finally:
        devnull.close()
    return True

and change it to be

def has_powershell():
     return False

ez_install.py will the use your environment HTTP_PROXY/HTTPS_PROXY which can be set from the command line or through the control panel.

Temporary Command Line:

set HTTP_PROXY=http://proxy.example.com
set HTTPS_PROXY=https://proxy.example.com

Note: if you do this you must run 'python ez_setup.py' in the same command window you ran these commands in.

Permanent Command Line (User Only):

setx HTTP_PROXY "http://proxy.example.com"
setx HTTPS_PROXY "https://proxy.example.com"

Permanent Command Line (Machine aka All Users):

setx HTTP_PROXY "http://proxy.example.com" /M
setx HTTPS_PROXY "https://proxy.example.com" /M

Permanent via Control Panel:

  1. Start -> Control Panel -> User Accounts
  2. On left panel, click "Change my environment variables"
  3. Click "New.." in "User variables" or "System Variables" (depending on what you want)
  4. Set Variable name: HTTP_PROXY and Variable value: http:/proxy.example.com
  5. Click "New.." in "User variables" or "System Variables" (depending on what you want)
  6. Set Variable name: HTTPS_PROXY and Variable value: https:/proxy.example.com
  7. Click 'Ok'
like image 22
Andrew Martinez Avatar answered Sep 28 '22 10:09

Andrew Martinez