[mod_python] Streaming output

Graham Dumpleton graham.dumpleton at gmail.com
Mon Apr 23 18:44:03 EDT 2007

On 24/04/07, Greg Fawcett <greg at vig.co.nz> wrote:
> Hi -
> Is there any way to stream output to a request object, instead of using
> req.write()? My application has to serve PDF documents (sometimes several
> tens of megabytes long). Access to these has to be controlled, and they are
> encrypted, so I can't just expose them as normal files to apache. PHP has a
> useful function passthru(command) which will run command and stream the
> output directly back to the client - is there any equivalent in python?
> On a related note, how "direct" is req.write()? I'd hate it to be buffering
> the whole PDF up somewhere along the line, before sending it to the client.

Check the documentation and I think you will find your answer.


For write():

write(string[, flush=1])
    Writes string directly to the client, then flushes the buffer,
unless flush is 0.

Thus, it defaults to flushing data as soon as written, but this can be
disabled and output will be buffered up until request is done.

sendfile(path[, offset, len])
    Sends len bytes of file path directly to the client, starting at
offset offset using the server's internal API. offset defaults to 0,
and len defaults to -1 (send the entire file).

    Returns the number of bytes sent, or raises an IOError exception on failure.

    This function provides the most efficient way to send a file to the client.

This passes the job of sending the file to Apache whereby it may be
able to use more efficient methods than you can use.


More information about the Mod_python mailing list