requests get response time and timeout

Request response time and timeout

Get response time (elapsed)

For the main methods and parameters commonly used in the requests library, you can refer to this article by Xiaoyu

7 main methods commonly used in requests library and control access parameters

Next, we will directly figure out how to obtain the response time, of course, using the elapsed method,
let's see how to view the method in elapsed :

import requests
re  = requests.get("https://blog.csdn.net/wuyoudeyuer")
help(re.elapsed)

The result of the operation is as shown in the figure below. A lot of content,
Insert picture description here
let’s take a small part directly. Next, let’s talk about the commonly used methods
·total_seconds total time in seconds
·days in days
·microseconds (>= 0 and less than 1 second) Get the microsecond part, greater than 0 and less than 1 second
・seconds Number of seconds (>= 0 and less than 1 day) seconds, greater than 0 and less than 1 day
・max = datetime.timedelta(999999999, 86399, 999999) maximum time
・min = datetime.timedelta(-999999999) minimum time
・resolution = datetime.timedelta(0, 0, 1) minimum time unit

Let’s take an example to see what the result looks like

import requests
r = requests.get("https://blog.csdn.net/wuyoudeyuer")
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)

The result is as follows, the request time is so fast...
Insert picture description here

Get timeout (timeout)

Request timed out, it’s very common. I won’t talk about it here. Just upload the code
and wait for a meeting...

import requests
r = requests.get("https://editor.csdn.net/md/?articleId=107375408", timeout=0.5)
print(r.elapsed)
print(r.elapsed.total_seconds())
print(r.elapsed.microseconds)

The results of the operation are as follows:
Insert picture description here
here to say:
・timeout is set to 0.5s,
・timeout, then this exception is thrown: requests.exceptions.ConnectTimeout: HTTPConnectionPool

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/107535975
Recommended