python urllib2 timeout处理实例,pythonurllib2,python的urlli


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

import urllib2import socketclass MyException(Exception):    passtry:    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        raiseexcept socket.timeout, e:    # For Python 2.7    raise MyException("There was an error: %r" % e)

评论关闭