Python interface Automation 20-requests Get response time (elapsed) and timeout (timeout)

Source: Internet
Author: User
Tags timedelta

Objective

Requests request, the interface response time, but also we need to pay attention to a point, if the response time is too long, it is unreasonable.
If the server does not respond in a timely manner, you can not wait until you set a timeout time.

About the response time of requests request, official online not too much introduction, and I Baidu search, read a lot of information written is r.elapsed.microseconds get, however are wrong!!!

Elapsed official documents
    1. Official document of the elapsed method address: 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. Use Help () to view the methods inside the elapsed

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

Elapsed inside several methods introduced

    • Total_seconds total duration, unit seconds

    • Days as a unit

    • Microseconds (>= 0 and less than 1 second) gets the microsecond portion, greater than 0 in 1 seconds

    • Seconds number of seconds (>= 0 and less than 1 days) seconds, greater than 0 for 1 day

    • max = Datetime.timedelta (999999999, 86399, 999999) max time

    • min = Datetime.timedelta (-999999999) min. time

    • Resolution = Datetime.timedelta (0, 0, 1) Min. unit of time

Get response time

1. Get elapsed different return values

import requestsr = 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. Online A lot of information is written to use microseconds to obtain response time, and in addition to 1000*1000 to get time for the units of seconds, when the request is less than 1s, found nothing wrong. If the time is more than 1s, the problem will come.
(obviously, more than 1s, only the number of decimal parts is intercepted)

3. So the correct posture to get response time should be: R.elapsed.total_seconds (), unit is S

Timeout timed out

1. If a request response time is longer, cannot wait, can set a timeout time, let it throw an exception

2. The following request, setting timeout of 0.5s, will throw this exception: Requests.exceptions.ConnectTimeout:HTTPConnectionPool

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

Python interface Automation 20-requests Get response time (elapsed) and timeout (timeout)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.