Python:HTTP 使用流式传输大文件 [英] Python: HTTP Post a large file with streaming

查看:99
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆