Python urllib2 进度挂钩
我正在尝试使用 urllib2 http 客户端在 python 中创建下载进度条。我查看了 API(以及谷歌),似乎 urllib2 不允许您注册进度挂钩。然而,较旧的已弃用的 urllib 确实具有此功能。
有谁知道如何使用 urllib2 创建进度条或报告挂钩?或者还有其他一些技巧可以获得类似的功能吗?
I am trying to create a download progress bar in python using the urllib2 http client. I've looked through the API (and on google) and it seems that urllib2 does not allow you to register progress hooks. However the older deprecated urllib does have this functionality.
Does anyone know how to create a progress bar or reporting hook using urllib2? Or are there some other hacks to get similar functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个完全有效的示例,它建立在 Anurag 响应分块方法的基础上。我的版本允许您设置块大小,并附加任意报告功能:
Here's a fully working example that builds on Anurag's approach of chunking in a response. My version allows you to set the the chunk size, and attach an arbitrary reporting function:
为什么不直接以块的形式读取数据并在其间做任何您想做的事情,例如在线程中运行、挂钩到 UI 等
输出:
Why not just read data in chunks and do whatever you want to do in between, e.g. run in a thread, hook into a UI, etc etc
output:
urlgrabber 内置了对进度通知的支持。
urlgrabber has built-in support for progress notification.
简化版:
Simplified version:
对 Triptych 的响应进行小修改,以允许实际写出文件(python3):
用法:
Minor modification to Triptych's response to allow for actually writing out the file (python3):
Usage: