我想捕获套接字超时(最好是异常)... except urllib.error.URLError:
可以捕获它但我需要区分死链接和超时....如果我取出{ {1}}套接字超时没有捕获,脚本终止时出现socket.timeout错误
except urllib.error.URLError:
答案 0 :(得分:5)
...
except urllib.error.URLError, e:
print type(e.reason)
只要有套接字超时,您就会看到<class 'socket.timeout'>
。这是你想要的吗?
例如:
try:
data = urllib2.urlopen("http://www.abcnonexistingurlxyz.com")
except Exception,e:
print type(e.reason)
...
<class 'socket.timeout'>