Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and proxy - urllib2.URLError: <urlopen error [Errno 110] Connection timed out>

Tags:

python

urllib2

I tried to google and search for similar question on stackOverflow, but still can't solve my problem.

I need my python script to perform http connections via proxy.
Below is my test script:

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()

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:

Traceback (most recent call last):
  File "proxy_auth.py", line 18, in <module>
    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: <urlopen error [Errno 110] Connection timed out>

I also tried to use requests library, and got the same error.

# testing request library
r = requests.get('http://www.whatismyip.com/', proxies={'http':'http://255.255.255.255:3128'})

If I don't set proxy, then the program works fine.

# this works fine
conn = urllib2.urlopen('http://www.whatismyip.com/')

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.

Are there any workarounds or alternative approaches that would let me set proxies for http connections? How should I modify my test script?

like image 439
Olexiy Avatar asked May 27 '13 14:05

Olexiy


1 Answers

The problem was in closed ports. I had to buy a dedicated IP before tech support could open the ports I needed.

Now my script works fine.

Conclusion: when you are on a shared hosting, most ports are probably closed and you will have to contact tech support to open ports.

like image 199
Olexiy Avatar answered Sep 18 '22 20:09

Olexiy