Python:HTTP发布带有流媒体的大文件 [英] Python: HTTP Post a large file with streaming
本文介绍了Python:HTTP发布带有流媒体的大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将可能较大的文件上传到网络服务器。目前我正在这样做:
I'm uploading potentially large files to a web server. Currently I'm doing this:
import urllib2
f = open('somelargefile.zip','rb')
request = urllib2.Request(url,f.read())
request.add_header("Content-Type", "application/zip")
response = urllib2.urlopen(request)
但是,这会在发布之前将整个文件的内容读入内存。如何将文件流式传输到服务器?
However, this reads the entire file's contents into memory before posting it. How can I have it stream the file to the server?
推荐答案
通过systempuntoout链接到的邮件列表线程,我找到了解决方案的线索。
Reading through the mailing list thread linked to by systempuntoout, I found a clue towards the solution.
mmap
模块允许您打开类似字符串的文件。文件的一部分按需加载到内存中。
The mmap
module allows you to open file that acts like a string. Parts of the file are loaded into memory on demand.
这是我现在使用的代码:
Here's the code I'm using now:
import urllib2
import mmap
# Open the file as a memory mapped string. Looks like a string, but
# actually accesses the file behind the scenes.
f = open('somelargefile.zip','rb')
mmapped_file_as_string = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
# Do the request
request = urllib2.Request(url, mmapped_file_as_string)
request.add_header("Content-Type", "application/zip")
response = urllib2.urlopen(request)
#close everything
mmapped_file_as_string.close()
f.close()
这篇关于Python:HTTP发布带有流媒体的大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文