0

I can ping and can also see google.com in a browser from the same machine, but when I try to use urllib2.urlopen(url) it fails. Why?

tmac:~ torobinson$ ping google.com
PING google.com (4.35.2.172): 56 data bytes
64 bytes from 4.35.2.172: icmp_seq=0 ttl=57 time=2.536 ms
64 bytes from 4.35.2.172: icmp_seq=1 ttl=57 time=1.447 ms
64 bytes from 4.35.2.172: icmp_seq=2 ttl=57 time=1.415 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 1.415/1.799/2.536/0.521 ms
tmac:~ torobinson$ python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> urllib2.urlopen('http://google.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
response = self._open(req, data)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 418, in _open
'_open', req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 378, in _call_chain
result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1207, in http_open
return self.do_open(httplib.HTTPConnection, req)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1177, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 61] Connection refused>

What could possibly cause this?

Edit: abarnert put me on the right path. Works once I install a ProxyHandler:

    proxy_support = urllib2.ProxyHandler({})
    opener = urllib2.build_opener(proxy_support)
    urllib2.install_opener(opener)
    response = urllib2.urlopen("http://google.com")
    print response.read()
Tony
  • 49
  • 3
  • 1
    Can you open google in your browser? Can you call it using `wget` in your command line? `ping` just tells you that the server is up. – Shashank Agarwal Sep 10 '14 at 17:09
  • 1
    The ping results aren't relevant here; the fact that you're getting ECONNREFUSED means that you were able to look up the IP address and reach the server so that it could refuse your connection--unless you have a firewall that intercepts and refuses outgoing connections, like LittleSnitch on your computer or something on your corporate network, in which case the ping just means the firewall is configured to block your HTTP connection but not your ICMP requests, which still doesn't tell you much. – abarnert Sep 10 '14 at 17:10
  • 1
    Meanwhile, does your browser have an HTTP proxy configured? If so, you almost certainly do have a firewall blocking port 80 outgoing, so you'll need to configure urlopen to use the same proxy your browser does. – abarnert Sep 10 '14 at 17:11
  • 1
    Anyway, for a more relevant test, try `nc google.com 80` or `echo -e 'GET / HTTP/1.0\r\n\r\n' | nc google.com 80`. If the connection is refused, your problem has nothing to do with your Python code. – abarnert Sep 10 '14 at 17:14

0 Answers0