python urllib2 超时处理实例

2015-08-13 0 998
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)

遇见资源网 c/c++ python urllib2 超时处理实例 http://www.ox520.com/10274.html

下一篇: KMP算法
常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务