title: Python and proxy - urllib2.URLError: <urlopen error [Errno 110] Connection timed out>
body: <p>I tried to google and search for similar question on stackOverflow, but still can't solve my problem.  </p>
  
  <p>I need my python script to perform http connections via proxy.<br>
  Below is my test script: </p>
  
  <pre><code>import urllib2, urllib
  
  proxy = urllib2.ProxyHandler({'http': 'http://255.255.255.255:3128'})
  opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
  urllib2.install_opener(opener)
  
  conn = urllib2.urlopen('http://www.whatismyip.com/')
  return_str = conn.read()
  
  webpage = open('webpage.html', 'w')
  webpage.write(return_str)
  webpage.close()
  </code></pre>
  
  <p>This script works absolutely fine on my local computer (Windows 7, Python 2.7.3), but when I try to run it on the server, it gives me the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "proxy_auth.py", line 18, in &lt;module&gt;
      conn = urllib2.urlopen('http://www.whatismyip.com/')
    File "/home/myusername/python/lib/python2.7/urllib2.py", line 126, in urlopen
      return _opener.open(url, data, timeout)
    File "/home/myusername/python/lib/python2.7/urllib2.py", line 400, in open
      response = self._open(req, data)
    File "/home/myusername/python/lib/python2.7/urllib2.py", line 418, in _open
      '_open', req)
    File "/home/myusername/python/lib/python2.7/urllib2.py", line 378, in _call_chai                                              n
      result = func(*args)
    File "/home/myusername/python/lib/python2.7/urllib2.py", line 1207, in http_open
      return self.do_open(httplib.HTTPConnection, req)
    File "/home/myusername/python/lib/python2.7/urllib2.py", line 1177, in do_open
      raise URLError(err)
  urllib2.URLError: &lt;urlopen error [Errno 110] Connection timed out&gt;
  </code></pre>
  
  <p>I also tried to use <a href="http://docs.python-requests.org/en/latest/index.html" rel="noreferrer">requests</a> library, and got the same error.</p>
  
  <pre><code># testing request library
  r = requests.get('http://www.whatismyip.com/', proxies={'http':'http://255.255.255.255:3128'})
  </code></pre>
  
  <p>If I don't set proxy, then the program works fine.</p>
  
  <pre><code># this works fine
  conn = urllib2.urlopen('http://www.whatismyip.com/')
  </code></pre>
  
  <p>I think the problem is that on my shared hosting account it is not possible to set an environment variable for proxy ... or something like that.</p>
  
  <p>Are there any workarounds or alternative approaches that would let me set proxies for http connections? How should I modify my test script?</p>