Stream a file to the HTTP response in Pylons

Posted by Evgeny on Stack Overflow See other posts from Stack Overflow or by Evgeny
Published on 2010-03-10T00:35:20Z Indexed on 2010/05/06 2:48 UTC
Read the original article Hit count: 339

Filed under:
|
|

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, this:

    with open(filepath, 'rb') as f:
        response.write(f.read())

That works, but it's obviously inefficient for large files. What's the best way to do this? I haven't been able to find any convenient methods in Pylons to stream the contents of the file. Do I really have to write the code to read a chunk at a time myself from scratch?

© Stack Overflow or respective owner

Related posts about pylons

Related posts about python