How to measure server response time for Python requests POST-request?

Shuzheng picture Shuzheng · Apr 6, 2017 · Viewed 50.9k times · Source

I create requests POST-requests like this, where I specify timeout threshold:

response = requests.post(url, data=post_fields, timeout=timeout)

However, to determine a "good" threshold value, I would like to benchmark the server response time in advance.

How do I compute the minimum and maximum response times for the server?

Answer

Nicolas Lykke Iversen picture Nicolas Lykke Iversen · Apr 6, 2017

The Response object as returned by requests.post() has a property called elapsed, which give the time delta between the Request was sent and the Response was received. To get the delta in seconds, use the total_seconds() method:

response = requests.post(url, data=post_fields, timeout=timeout)
print(response.elapsed.total_seconds())

It should be mentioned that requests.post() is a synchronous operation, which means that it "blocks" until the response is received.