[Web-SIG] serving (potentially large) files through wsgi?

Manlio Perillo manlio_perillo at libero.it
Fri Dec 14 17:56:33 CET 2007

Chris Withers ha scritto:
> Manlio Perillo wrote:
>> Note however that this is an optional feature, so a wsgi gateway is 
>> not required to implement it.
> What happens if the app is expecting the gateway to support it and the 
> gateway does not?

You can still use something like:

class FileWrapper(object):
     def __init__(self, fp, blksize=8192):
         self.fp = fp

     def __getitem__(self, key):
         data = self.fp.read(self.blksize)
         if data:
             return data

         raise IndexError

     def close(self):

The wsgi gateway will iterate over the FileWrapper instance.

>>> Also, does wsgi offer anything to help with http range requests and 
>>> the like?
>> No.
>> For dynamic resources this is not easy to handle, and for static 
>> resources you can delegate the job to the web server.
> Yeah, but I'm thinking of thinks like big pdfs stored in an object 
> database...

There are 3 solutions, as far as I know:

1) reading the whole data and let the server return to the client only
    the requested range.

    This is possible if you use a WSGI implementation embedded in a web
    server like Apache or Nginx.

    For mod_wsgi for Nginx I have implemented this in:

    However the current implementation of nginx range filter only
    supports single bufferred response.
    That is, you have to read the data at once, or use wsgi.file_wrapper
    (not yet implemented, and it will only work for files), so this is
    not a good solution for big data stored in a object database

2) handle the range request in the WSGI application.
    Its not hard as long as you do not implement multiple ranges support.

    If your object database supports seeks, this should be the most
    efficient solution.

3) the same as 1), but with range support implemented in a WSGI gateway

> cheers,
> Chris

Manlio Perillo

More information about the Web-SIG mailing list