connection - Python's urllib2 won't connect... to anything -
i can ping , can see google.com in browser same machine, when try use urllib2.urlopen(url) fails. why?
tmac:~ torobinson$ ping google.com ping google.com (4.35.2.172): 56 data bytes 64 bytes 4.35.2.172: icmp_seq=0 ttl=57 time=2.536 ms 64 bytes 4.35.2.172: icmp_seq=1 ttl=57 time=1.447 ms 64 bytes 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" 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 possibly cause this?
edit: abarnert put me on right path. works once install proxyhandler:
proxy_support = urllib2.proxyhandler({}) opener = urllib2.build_opener(proxy_support) urllib2.install_opener(opener) response = urllib2.urlopen("http://google.com") print response.read()
Comments
Post a Comment