« »
2008-12-21Python

62

python: 检查某个URL连接是否超时

  1. #检查某个URL连接是否超时
  2. import socket
  3. import urllib2
  4.  
  5. def timeout(site, timeout):
  6.     save = socket.getdefaulttimeout() 
  7.     try:
  8.       response = urllib2.urlopen(site)
  9.       socket.setdefaulttimeout(save)
  10.     except urllib2.URLError, err:
  11.       socket.setdefaulttimeout(save)
  12.       if err.__class__.__name__ == "URLError":
  13.         if isinstance(err[0], socket.timeout):
  14.           return True
  15.       return False
  16.     if timeout("http://www.linuxlaptop.cn/index.php/archives/700", 10):
  17.        print "Timeout detected"

您还可能感兴趣的内容

日志信息 »

该日志于2008-12-21 18:18由 admin 发表在Python分类下, 你可以发表评论。除了可以将这个日志以保留源地址及作者的情况下引用到你的网站或博客,还可以通过RSS 2.0订阅这个日志的所有评论。

没有评论

发表评论 »


返回顶部