| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
gd7g
10年前发布

python urllib2 超时处理实例

python的urllib2包的timeout异常需要通过socket.timeout异常来捕获,如下实例:

import urllib2  import socket    class MyException(Exception):      pass    try:      urllib2.urlopen("http://example.com", timeout = 1)  except urllib2.URLError, e:      # For Python 2.6      if isinstance(e.reason, socket.timeout):          raise MyException("There was an error: %r" % e)      else:          # reraise the original error          raise  except socket.timeout, e:      # For Python 2.7      raise MyException("There was an error: %r" % e)