urllib2 POST 进度监控
我正在通过 POST 使用 urllib2 将一个相当大的文件上传到服务器端脚本。我想显示一个进度指示器,显示当前上传进度。 urllib2 是否提供了一个钩子或回调来让我监控上传进度?我知道您可以通过连续调用连接的 read() 方法来进行下载,但我没有看到 write() 方法,您只需将数据添加到请求中即可。
I'm uploading a fairly large file with urllib2 to a server-side script via POST. I want to display a progress indicator that shows the current upload progress. Is there a hook or a callback provided by urllib2 that allows me to monitor upload progress? I know that you can do it with download using successive calls to the connection's read() method, but I don't see a write() method, you just add data to the request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是可能的,但您需要做一些事情:
read()
方法:当 httplib 调用read()
时,您的回调将被调用,让您计算百分比并更新进度条。这可以与任何类似文件的对象一起使用,但我包装了
file
以展示它如何与从磁盘流式传输的非常大的文件一起使用:输出:
It is possible but you need to do a few things:
__len__
attribute which makeslen(data)
return the correct size, used to populate the Content-Length header.read()
method on your file handle: as httplib callsread()
your callback will be invoked, letting you calculate the percentage and update your progress bar.This could work with any file-like object, but I've wrapped
file
to show how it could work with a really large file streamed from disk:Output:
requests 2.0.0 具有流式上传。这意味着您可以使用生成器来生成微小的块并打印块之间的进度。
requests 2.0.0 has streaming uploads. This means you can use a generator to yield tiny chunks and print the progress between chunks.
我认为这是不可能的,但是 pycurl 确实有上传/下载进度回调可以使用。
I don't think this is possible, but pycurl does have upload/download progress callbacks you can use.
海报 支持此功能
poster supports this