python: 检查某个URL连接是否超时
- #检查某个URL连接是否超时
- import socket
- import urllib2
- def timeout(site, timeout):
- save = socket.getdefaulttimeout()
- try:
- response = urllib2.urlopen(site)
- socket.setdefaulttimeout(save)
- except urllib2.URLError, err:
- socket.setdefaulttimeout(save)
- if err.__class__.__name__ == "URLError":
- if isinstance(err[0], socket.timeout):
- return True
- return False
- if timeout("http://www.linuxlaptop.cn/index.php/archives/700", 10):
- print "Timeout detected"
没有评论▼