前言

requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的。
如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间

关于requests请求的响应时间,官网上没太多介绍,并且我百度搜了下,看很多资料写的是r.elapsed.microseconds获取的,然而都是错的!!!

elapsed官方文档

  1. elapsed方法的官方文档地址:http://cn.python-requests.org/zh_CN/latest/api.html#requests.Response
requests.Response

     elapsed = None

     The amount of time elapsed between sending the request and the arrival of the response (as a timedelta). This property specifically measures the time taken between sending the first byte of the       request and finishing parsing the headers. It is therefore unaffected by consuming the response content or the value of the stream keyword argument.
简单翻译:计算的是从发送请求到服务端响应回来这段时间(也就是时间差),发送第一个数据到收到最后一个数据之间,这个时长不受响应的内容影响

2.用help()查看elapsed里面的方法

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

elapsed里面几个方法介绍

  • total_seconds 总时长,单位秒

  • days 以天为单位

  • microseconds (>= 0 and less than 1 second) 获取微秒部分,大于0小于1秒

  • seconds Number of seconds (>= 0 and less than 1 day) 秒,大于0小于1天

  • max = datetime.timedelta(999999999, 86399, 999999) 最大时间

  • min = datetime.timedelta(-999999999) 最小时间

  • resolution = datetime.timedelta(0, 0, 1) 最小时间单位

获取响应时间

1.获取elapsed不同的返回值

import requests
r = requests.get("http://www.cnblogs.com/yoyoketang/")
print(r.elapsed)
print(r.elapsed.total_seconds())
print(r.elapsed.microseconds)
print(r.elapsed.seconds)
print(r.elapsed.days)
print(r.elapsed.max)
print(r.elapsed.min)
print(r.elapsed.resolution)

2.网上很多资料写的是用microseconds获取响应时间,再除1000*1000得到时间为秒的单位,当请求小于1s时,发现不出什么问题。如果时间超过1s,问题就来了。
(很显然,大于1s的时候,只截取了后面的小数部分)

3.所以获取响应时间的正确姿势应该是:r.elapsed.total_seconds(),单位是s

timeout超时

1.如果一个请求响应时间比较长,不能一直等着,可以设置一个超时时间,让它抛出异常

2.如下请求,设置超时为0.5s,那么就会抛出这个异常:requests.exceptions.ConnectTimeout: HTTPConnectionPool

import requests
r = requests.get("http://cn.python-requests.org/zh_CN/latest/", timeout=1)
print(r.elapsed)
print(r.elapsed.total_seconds())
print(r.elapsed.microseconds)

来源: http://www.cnblogs.com/yoyoketang/p/8035428.html

python接口自动化20-requests获取响应时间(elapsed)与超时(timeout) ok试了 获取响应时间的的更多相关文章

  1. python接口自动化测试之requests库详解

    前言 说到python发送HTTP请求进行接口自动化测试,脑子里第一个闪过的可能就是requests库了,当然python有很多模块可以发送HTTP请求,包括原生的模块http.client,urll ...

  2. Python接口自动化【requests处理Token请求】

    首先说一下使用python模拟登录或注册时,对于带token的页面怎么登录注册模拟的思路: 1.对于带token的页面,需要先从最开始的页面获取合法token 2.然后使用获取到的合法token进行后 ...

  3. python接口自动化:requests+ddt+htmltestrunner数据驱动框架

    该框架分为四个包:xc_datas.xc_driven.xc_report.xc_tools. xc_datas:存放数据,xc_driven:存放执行程序,xc_report:存放生成的报告,xc_ ...

  4. request的响应时间elapsed和超时timeout

    前言:requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的 1.获取接口请求的响应时间  r.elapsed.total_seconds() import ...

  5. python接口自动化20-requests获取响应时间(elapsed)与超时(timeout)

    前言 requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的. 如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 关于reques ...

  6. python接口自动化(十)--post请求四种传送正文方式(详解)

    简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...

  7. python接口自动化-Cookie_绕过验证码登录

    前言 有些登录的接口会有验证码,例如:短信验证码,图形验证码等,这种登录的验证码参数可以从后台获取(或者最直接的可查数据库) 获取不到也没关系,可以通过添加Cookie的方式绕过验证码 前面在“pyt ...

  8. python接口自动化28-requests-html爬虫框架

    前言 requests库的好,只有用过的人才知道,最近这个库的作者又出了一个好用的爬虫框架requests-html.之前解析html页面用过了lxml和bs4, requests-html集成了一些 ...

  9. python接口自动化-参数化

    原文地址https://www.cnblogs.com/yoyoketang/p/6891710.html python接口自动化 -参数关联(一)https://www.cnblogs.com/11 ...

  10. python接口自动化 -参数关联(一)

    原文地址https://www.cnblogs.com/yoyoketang/p/6886610.html 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址ht ...

随机推荐

  1. 将button或者input角变为圆弧

    style="border-radius:5px;" input框时,不能用type属性

  2. WINDOWS系统的正确安装-硬盘格式如何选择

    有一种这样的说法,WIN7改装WIN10必须要重新分区,将硬盘格式化为GPT格式(GUID分区表 ), WIN10改装WIN7必须要重新分区,将硬盘格式化为MBR格式. 这种说法一直困扰着我,于是经过 ...

  3. stm32l071cbt6片内flash操作

    今天在看片内flash的操作,发现按照下面的操作并没有写成功: unsigned long temp = 0x12345678; HAL_FLASH_Unlock(); FLASH_PageErase ...

  4. ionic1页面间传递参数的问题

    1.  $scope.routeinfo是我要传递的参数--到scheddulcontent这个页面去: $state.go( "scheddulcontent" , { 'rou ...

  5. HNOI2019 简要题解

    HNOI 2019 简要题解 没想到自己竟也能有机会写下这篇题解呢. LOJ Luogu Day1T1 鱼 枚举\(AD\)两点后发现\(BC\)与\(EF\)相对独立,因此只需要计算合法的\(BC\ ...

  6. 【BZOJ2229】【ZJOI2011】最小割

    冷门知识点…… 原题: 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中 ...

  7. centos7配置lamp成功安装过

    linux+apache+mysql/mariadb+php 首先apache的安装: yum install httpd 接着mysql/mariadb的安装: yum install mysql ...

  8. Behavior Designer 学习

    http://www.opsive.com/ 简单Demo Sequence Selector Chase Enemy enemy:player:

  9. div中 li宽度不固定 ie6和ie7不兼容不自动换行

    我的li因为内容字数不一样,所以宽度不固定,给他float:left属性后,ie6和ie7不兼容,不自动换行!我给ul或者li: ul{white-space: nowrap} 属性还是不管用..最后 ...

  10. Reactor/Proactor的比较 (ZZ)

    一般情况下,I/O 复用机制需要事件分享器(event demultiplexor [1.3]). 事件分享器的作用,即将那些读写事件源分发给各读写事件的处理者,就像送快递的在楼下喊: 谁的什么东西送 ...