How to measure server response time for Python requests POST-request(如何测量 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()
(和requests.get()
etc.) 有一个名为 elapsed
的属性,它提供发送 Request
和收到 Response
之间的时间差.要以秒为单位获取增量,请使用 total_seconds()
方法:
The Response
object returned by requests.post()
(and requests.get()
etc.) has a property called elapsed
, which provides 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()
是一个同步操作,这意味着它阻塞直到收到 Response
.
Note that requests.post()
is a synchronous operation, which means that it blocks until the Response
is received.
这篇关于如何测量 Python 请求 POST 请求的服务器响应时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何测量 Python 请求 POST 请求的服务器响应时间


基础教程推荐
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01