在django应用程序中,我使用第三方Python脚本允许用户通过httplib将文件上传到blip.tv。EC2实例上的HTTPConnection.send。由于这些文件通常很大,我将使用消息队列异步处理上传(RabbitMQ/Celery),并在前端向用户反馈进度。
httpconnection和send在脚本的这一部分中完成:
host, selector = urlparts = urlparse.urlsplit(url)[1:3]
h = httplib.HTTPConnection(host)
h.putrequest("POST", selector)
h.putheader("content-type", content_type)
h.putheader("content-length", len(data))
h.endheaders()
h.send(data)
response = h.getresponse()
return response.status, response.reason, response.read()
getresponse()在文件传输完成后返回,我如何写出进度(假设使用stdout.write),以便将此值写入缓存框架以进行显示(djangosippets 678/679)?或者,如果有更好的练习,我洗耳恭听!
编辑:
由于我使用了urllib2,并使用了这个问题中的提示来覆盖文件的read()以获取上传进度。此外,我正在使用poster生成多部分urlencode。这是最新的代码:
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
def Upload(video_id, username, password, title, description, filename):
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
progress = Progress()
stream = file_with_callback(filename, 'rb', progress.update, filename)
datagen, headers = multipart_encode({
"post": "1",
"skin": "xmlhttprequest",
"userlogin": "%s" % username,
"password": "%s" % password,
"item_type": "file",
"title": "%s" % title.encode("utf-8"),
"description": "%s" % description.encode("utf-8"),
"file": stream
})
opener = register_openers()
req = urllib2.Request(UPLOAD_URL, datagen, headers)
response = urllib2.urlopen(req)
return response.read()
这只适用于文件路径输入,而不是来自表单输入(request.FILES)的InMemoryUploadedFile,因为我想它正在尝试读取已经保存在内存中的文件,并且我在第行得到了一个TypeError:"stream=file_with_callback(filename,'rb',progress.update,filename)":
coercing to Unicode: need string or buffer, InMemoryUploadedFile found
如何使用用户上传的文件实现相同的进度报告?此外,这样读取进度会消耗大量内存吗?也许urllib2的下载进度上传解决方案会更好,但如何实现。。。帮助是非常受欢迎的
事实证明,海报库在multipart_encode中有一个回调挂钩,可以用来获取进度(上传或下载)。好东西。。。
虽然我想我从技术上回答了这个问题,但我相信还有其他方法可以剥这只猫的皮,所以如果我找到其他方法或细节,我会发布更多。
这是代码:
def prog_callback(param, current, total):
pct = 100 - ((total - current ) *100 )/ (total)
print "Progress: %s " % pct
datagen, headers = multipart_encode({
"post": "1",
"skin": "xmlhttprequest",
"userlogin": "%s" % username,
"password": "%s" % password,
"item_type": "file",
"title": "%s" % title.encode("utf-8"),
"description": "%s" % description.encode("utf-8"),
"file": filename
}, cb=prog_callback)
opener = register_openers()
req = urllib2.Request(UPLOAD_URL, datagen, headers)
response = urllib2.urlopen(req)
return response.read()