WebOb File-Serving Example
Note
Starting from 1.2b4, WebOb ships with a webob.static module which implements a WSGI application similar to the one described below.
This document stays as a didactic example how to serve files with WebOb, but you should consider using applications from webob.static in production.
First we’ll setup a really simple shim around our application, which we can use as we improve our application:
>>> def make_response(filename):
... res = Response(content_type=get_mimetype(filename))
... res.body = open(filename, 'rb').read()
... return res
We’ll test it out with a file test-file.txt
in the WebOb doc directory, which has the following content:
This is a test. Hello test people!
Let’s give it a shot:
Well, that worked. But it’s not a very fancy object. First, it reads everything into memory, and that’s bad. We’ll create an iterator instead:
>>> class FileIterable(object):
... def __init__(self, filename):
... self.filename = filename
... def __iter__(self):
... return FileIterator(self.filename)
>>> class FileIterator(object):
... chunk_size = 4096
... def __init__(self, filename):
... self.filename = filename
... self.fileobj = open(self.filename, 'rb')
... def __iter__(self):
... return self
... def next(self):
... chunk = self.fileobj.read(self.chunk_size)
... if not chunk:
... raise StopIteration
... return chunk
... __next__ = next # py3 compat
>>> def make_response(filename):
... res = Response(content_type=get_mimetype(filename))
... res.app_iter = FileIterable(filename)
... res.content_length = os.path.getsize(filename)
... return res
And testing:
>>> print req.get_response(app)
Content-Type: text/plain; charset=UTF-8
Content-Length: 35
This is a test. Hello test people!
Now, with conditional_response
on, and with last_modified
and etag
set, we can do conditional requests:
>>> req = Request.blank('/')
>>> res = req.get_response(app)
>>> print res
200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 35
Last-Modified: ... GMT
ETag: ...-...
This is a test. Hello test people!
>>> req2 = Request.blank('/')
>>> req2.if_none_match = res.etag
>>> req2.get_response(app)
<Response ... 304 Not Modified>
>>> req3 = Request.blank('/')
>>> req3.if_modified_since = res.last_modified
>>> req3.get_response(app)
<Response ... 304 Not Modified>
We can even do Range requests, but it will currently involve iterating through the file unnecessarily. When there’s a range request (and you set conditional_response=True
) the application will satisfy that request. But with an arbitrary iterator the only way to do that is to run through the beginning of the iterator until you get to the chunk that the client asked for. We can do better because we can use fileobj.seek(pos)
to move around the file much more efficiently.
So we’ll add an extra method, app_iter_range
, that Response
looks for:
>>> class FileIterable(object):
... def __init__(self, filename, start=None, stop=None):
... self.start = start
... def __iter__(self):
... return FileIterator(self.filename, self.start, self.stop)
... def app_iter_range(self, start, stop):
... return self.__class__(self.filename, start, stop)
>>> class FileIterator(object):
... chunk_size = 4096
... def __init__(self, filename, start, stop):
... self.filename = filename
... self.fileobj = open(self.filename, 'rb')
... if start:
... self.fileobj.seek(start)
... if stop is not None:
... self.length = stop - start
... else:
... self.length = None
... def __iter__(self):
... return self
... def next(self):
... if self.length is not None and self.length <= 0:
... raise StopIteration
... chunk = self.fileobj.read(self.chunk_size)
... if not chunk:
... raise StopIteration
... if self.length is not None:
... self.length -= len(chunk)
... if self.length < 0:
... # Chop off the extra:
... chunk = chunk[:self.length]
Now we’ll test it out: