Python';s urllib2韩元';t连接。。。做任何事
我可以ping,也可以在同一台机器的浏览器中看到google.com,但当我尝试使用urllib2.urlopen(url)时,它失败了。为什么?Python';s urllib2韩元';t连接。。。做任何事,python,connection,urllib2,urlopen,Python,Connection,Urllib2,Urlopen,我可以ping,也可以在同一台机器的浏览器中看到google.com,但当我尝试使用urllib2.urlopen(url)时,它失败了。为什么? 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 tt
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>
你能在浏览器中打开谷歌吗?您可以在命令行中使用
wget
调用它吗ping
只是告诉您服务器已启动。ping结果与此无关;事实上,您正在获得ECONREFUNCE意味着您可以查找IP地址并访问服务器,以便服务器可以拒绝您的连接--除非您有一个防火墙,可以拦截并拒绝传出连接,例如您计算机上的LittleSnitch或公司网络上的某个东西,在这种情况下,ping仅仅意味着防火墙被配置为阻止您的HTTP连接,而不是阻止您的ICMP请求,这仍然不能告诉您多少。同时,您的浏览器是否配置了HTTP代理?如果是这样的话,您几乎肯定有防火墙阻止端口80传出,因此您需要配置urlopen以使用与浏览器相同的代理。无论如何,要进行更相关的测试,请尝试nc google.com 80
或echo-e'GET/HTTP/1.0\r\n\r\n'|nc google.com 80
。如果连接被拒绝,那么问题与Python代码无关。
proxy_support = urllib2.ProxyHandler({})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
response = urllib2.urlopen("http://google.com")
print response.read()