网络超时

  在访问一个网页时,如果该网页长时间未响应,系统会判断该网页超时,所以无法打开网页。下面通过代码来模拟一个网络超时的现象,如下:

import requests

for a in range(1,50):
    try:
        #
        response=requests.get('https://www.baidu.com/',timeout=0.05)
        print(response.status_code)
    except Exception as e:
        print('异常'+str(e))

 

说起网络异常信息,requsets模块提供了三种常见的网络异常类,如下:

import requests

from requests.exceptions import ReadTimeout,HTTPError,RequestException

for a in range(1,50):
    try:
        response=requests.get('https://www.baidu.com/',timeout=0.05)
        print(response.status_code)
    except ReadTimeout:
        print('timeout')
    except HTTPError:
        print('httperror')
    except RequestException:
        print('reqerror')

 

posted @ 2019-02-27 10:32  余火灬  阅读(274)  评论(0编辑  收藏  举报