1

我在 python 中编写了一个下载函数。文件大小>1GB。服务器是 linux,HTTP 服务器是 Karrigell。客户端是浏览器、Firefox 或 IE。我遇到了大麻烦。

起初,我使用 sys.stdout() 来发送文件内容。

file = open(path, 'rb')
size = os.path.getsize(path)

RESPONSE['Pragma'] = 'public'
RESPONSE['Expires'] = '0'
RESPONSE['Cache-Control'] = 'must-revalidate, pre-check=0'
RESPONSE['Content-Disposition'] = 'attachment; filename="' + os.path.basename(path) + '"'
RESPONSE['Content-type'] = "application/octet-stream"
RESPONSE['Content-Transfer-Encoding'] = 'binary'
RESPONSE['Content-length'] = str(os.path.getsize(path))

sys.stdout.flush()
chunk_size = 10000
handle = open(path, "rb")
while True:
    buffer = handle.read(chunk_size)
    if buffer:
        STDOUT(buffer)
    else:
        break
sys.stdout.flush()

问题是服务器内存不足!我知道,stdout 首先将内容写入内存,然后将内存发送到套接字。

所以,我修改了函数。直接将内容发送到套接字。我使用 py-sendfile 模块。http://code.google.com/p/py-sendfile/

file = open(path, 'rb')
size = os.path.getsize(path)

sock = REQUEST_HANDLER.sock
sock.sendall("""HTTP/1.1 200 OK\r\nPragma: no-cache\r\nExpires: 0\r\nCache-Control: no-cache, no-store\r\nContent-Disposition: attachment; filename="%s"\r\nContent-Type: application/octet-stream\r\nContent-Length: %u\r\nContent-Range: bytes 0-4096/%u\r\nLocation: "%s"\r\n\r\n""" % (os.path.basename(path), size, size, os.path.basename(path)))

offset = 0
nbytes = 4096
while 1:
    try:
        sent = sendfile.sendfile(sock.fileno(), file.fileno(), offset, nbytes)
    except OSError, err:
        if err.errno in (errno.EAGAIN, errno.EBUSY):  # retry
            continue
        raise
    else:
        if sent == 0:
            break    # done
        offset += sent

这次,服务器内存还可以,但是浏览不行!浏览内存迅速上升!在套接字接受整个文件内容之前不是免费的。

我不知道如何处理这些问题。我觉得第二种思路是对的,直接向socket发送内容。但是为什么浏览器在接受数据时不能释放内存呢?

4

1 回答 1

1

您应该尝试分块下载文件。这是一个使用 urllib2 对我有用的示例

import os
import urllib2
import math

def downloadChunks(url):
    """Helper to download large files
        the only arg is a url
       this file will go to a temp directory
       the file will also be downloaded
       in chunks and print out how much remains
    """

    baseFile = os.path.basename(url)

    #move the file to a more uniq path
    os.umask(0002)
    temp_path = "/tmp/"
    try:
        file = os.path.join(temp_path,baseFile)

        req = urllib2.urlopen(url)
        total_size = int(req.info().getheader('Content-Length').strip())
        downloaded = 0
        CHUNK = 256 * 10240
        with open(file, 'wb') as fp:
            while True:
                chunk = req.read(CHUNK)
                downloaded += len(chunk)
                print math.floor( (downloaded / total_size) * 100 )
                if not chunk: break
                fp.write(chunk)
    except urllib2.HTTPError, e:
        print "HTTP Error:",e.code , url
        return False
    except urllib2.URLError, e:
        print "URL Error:",e.reason , url
        return False

    return file
于 2011-12-04T18:48:08.703 回答