urllib.error

import socket
from urllib import request,error
try:
    response = request.urlopen('https://cuiqingcai.com/index/htm',timeout=0.01)
except error.HTTPError as e:
    #先捕获子类错误HTTPError
    print(e.reason,e.code,e.headers,sep='\n')
except error.URLError as e:    
    #再捕获父类错误URLError
    print(type(e.reason))  #reason有时返回对象
    print(e.reason)
    if isinstance(e.reason,socket.timeout):
        print('TIMEOUT')
else:
    print('Request Successfully')
相关文章
相关标签/搜索