如何测量Python请求POST请求的服务器响应时间? [英] How to measure server response time for Python requests POST-request?

查看:701
本文介绍了如何测量Python请求POST请求的服务器响应时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建这样的requests POST请求,并在其中指定超时阈值:

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?

推荐答案

requests.post()返回的Response对象具有一个名为elapsed的属性,该属性给出了Request发送与发送之间的时间差.接收到Response.要以秒为单位获取增量,请使用total_seconds()方法:

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())

应该提到的是requests.post()是同步操作,这意味着它阻塞"直到收到响应.

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

这篇关于如何测量Python请求POST请求的服务器响应时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆