Skip to content Skip to sidebar Skip to footer

Stream A File To The Http Response In Pylons

I have a Pylons controller action that needs to return a file to the client. (The file is outside the web root, so I can't just link directly to it.) The simplest way is, of course

Solution 1:

The correct tool to use is shutil.copyfileobj, which copies from one to the other a chunk at a time.

Example usage:

import shutil
withopen(filepath, 'r') as f:
    shutil.copyfileobj(f, response)

This will not result in very large memory usage, and does not require implementing the code yourself.

The usual care with exceptions should be taken - if you handle signals (such as SIGCHLD) you have to handle EINTR because the writes to response could be interrupted, and IOError/OSError can occur for various reasons when doing I/O.

Solution 2:

I finally got it to work using the FileApp class, thanks to Chris AtLee and THC4k (from this answer). This method also allowed me to set the Content-Length header, something Pylons has a lot of trouble with, which enables the browser to show an estimate of the time remaining.

Here's the complete code:

def_send_file_response(self, filepath):
    user_filename = '_'.join(filepath.split('/')[-2:])
    file_size = os.path.getsize(filepath)

    headers = [('Content-Disposition', 'attachment; filename=\"' + user_filename + '\"'),
               ('Content-Type', 'text/plain'),
               ('Content-Length', str(file_size))]

    from paste.fileapp import FileApp
    fapp = FileApp(filepath, headers=headers)

    return fapp(request.environ, self.start_response)

Solution 3:

The key here is that WSGI, and pylons by extension, work with iterable responses. So you should be able to write some code like (warning, untested code below!):

deffile_streamer():
    withopen(filepath, 'rb') as f:
        whileTrue:
            block = f.read(4096)
            ifnot block:
                breakyield block
response.app_iter = file_streamer()

Also, paste.fileapp.FileApp is designed to be able to return file data for you, so you can also try:

return FileApp(filepath)

in your controller method.

Post a Comment for "Stream A File To The Http Response In Pylons"