我想知道如何使用 C++ 中的 curl 库实时检查文件上传的速度。这是我写的:
curl_easy_getinfo(curl,CURLINFO_SPEED_UPLOAD,&c);
但是手册上说它显示的是平均速度,但即使这样似乎对我也不起作用,因为我只能看到一个 0。
还有一件事:如何设置有效的上传限制,因为如果我这样写:
curl_easy_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE, 100);
我收到错误 502 消息。
最佳答案
来自curl_easy_getinfo
文档:
CURLINFO_SPEED_UPLOAD
Pass a pointer to a double to receive the average upload speed that curl measured for the complete [emphasis added] upload. Measured in bytes/second.
CURLINFO_SPEED_UPLOAD
仅在上传完成后才有效。相反,编写一个进度回调并设置 CURLOPT_PROGRESSFUNCTION
.让你的进度函数计算exponential moving average对于当前速度。
progress(curl, fd, len):
now = time()
speed = len/(now-then) * weight + speed * (1-weight)
update progress display
then=now
一如既往,网络速度是一个近似值。
关于c++ - curl 连接选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2614948/