前言:requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的

1、获取接口请求的响应时间  r.elapsed.total_seconds()

import requests
r = requests.get("https://www.baidu.com")
print(r.elapsed.total_seconds())

运行结果:

2、设置接口请求的超时时间  timeout=float(timeout)

    def get(self):
        try:
            r = requests.get(self.url, headers=self.headers, params=self.params, timeout=float(timeout))
            print(r.url)
            result = decodeToBase64(r.content)
            response = json.loads(result)
            # response.raise_for_status()
            return response
        except TimeoutError:
            self.logger.error("Time out!")
            return None