问题描述
我正在通过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.
推荐答案
这是可能的,但你需要做一些事情:
It is possible but you need to do a few things:
- 假urllib2子系统通过附加
__ len __
属性将文件句柄传递给httplib,这使得len(data)
返回正确的大小,用于填充Content-Length标头。 - 覆盖文件句柄上的
read()
方法:as httplib调用read()
将调用您的回调,让您计算百分比并更新进度条。
- Fake out the urllib2 subsystem into passing a file handle down to httplib by attaching a
__len__
attribute which makeslen(data)
return the correct size, used to populate the Content-Length header. - Override the
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:
import os, urllib2
from cStringIO import StringIO
class Progress(object):
def __init__(self):
self._seen = 0.0
def update(self, total, size, name):
self._seen += size
pct = (self._seen / total) * 100.0
print '%s progress: %.2f' % (name, pct)
class file_with_callback(file):
def __init__(self, path, mode, callback, *args):
file.__init__(self, path, mode)
self.seek(0, os.SEEK_END)
self._total = self.tell()
self.seek(0)
self._callback = callback
self._args = args
def __len__(self):
return self._total
def read(self, size):
data = file.read(self, size)
self._callback(self._total, len(data), *self._args)
return data
path = 'large_file.txt'
progress = Progress()
stream = file_with_callback(path, 'rb', progress.update, path)
req = urllib2.Request(url, stream)
res = urllib2.urlopen(req)
输出:
large_file.txt progress: 0.68
large_file.txt progress: 1.36
large_file.txt progress: 2.04
large_file.txt progress: 2.72
large_file.txt progress: 3.40
...
large_file.txt progress: 99.20
large_file.txt progress: 99.87
large_file.txt progress: 100.00
这篇关于urllib2 POST进度监控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!