Python urllib存在问题

4
每当我尝试使用urllib.urlopen下载网页时,就会出现错误。我已经关闭了Windows防火墙和杀毒软件,所以不是这个问题。我可以在浏览器中访问页面。我甚至重新安装了Python来排除是损坏的urllib引起的问题。非常感谢您的帮助。
>>> import urllib
>>> h = urllib.urlopen("http://www.google.com").read()

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    h = urllib.urlopen("http://www.google.com").read()
  File "C:\Python26\lib\urllib.py", line 86, in urlopen
    return opener.open(url)
  File "C:\Python26\lib\urllib.py", line 205, in open
    return getattr(self, name)(url)
  File "C:\Python26\lib\urllib.py", line 344, in open_http
    h.endheaders()
  File "C:\Python26\lib\httplib.py", line 904, in endheaders
    self._send_output()
  File "C:\Python26\lib\httplib.py", line 776, in _send_output
    self.send(msg)
  File "C:\Python26\lib\httplib.py", line 735, in send
    self.connect()
  File "C:\Python26\lib\httplib.py", line 716, in connect
    self.timeout)
  File "C:\Python26\lib\socket.py", line 514, in create_connection
    raise error, msg
IOError: [Errno socket error] [Errno 10061] No connection could be made because the target machine actively refused it
>>> 

对我来说可以工作,可能是你向谷歌发送了过多的请求,导致他们暂时切断了你的访问。 - catchmeifyoutry
不,这是发生在我尝试加载的任何URL上。 - mudder
你使用Linux吗?尝试运行strace python yourscript.pl,看看会得到什么。 - Makis
我在使用Windows,但我有straceNT。只是我不太知道如何阅读它 ;/ - mudder
这是它停止的位置的粘贴:http://www.pastie.org/968265 - mudder
显示剩余2条评论
2个回答

5

可能是这种情况

我发现问题出在我设置了一个代理,但是那个代理已经离线了,所以我的Python Shell也无法使用。


是的,就是这样。显然,Idle使用Internet选项中的代理设置。非常感谢Mykhal。 - mudder

0

urllib工作得非常好。

尝试在您的计算机上使用Ethereal(或类似的网络嗅探器)来确定拒绝是来自您的计算机还是来自其他计算机。


它不可能是机器出了问题,因为我的浏览器可以访问我尝试的网站。而且每个我尝试的网站都出现这种情况,所以不是用户代理或类似的问题。 - mudder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接