[Chicago] help from Django and Pylons developers

Ian Bicking ianb at colorstudy.com
Mon Apr 28 23:11:39 CEST 2008


Massimo Di Pierro wrote:
> Sorry the web2py way is better. Here is the complete code
> 
> #in applications/security/controller/default.py
> class Jammer():
>   def read(self,n):  return 'x'*n
> def jam():  return response.stream(Jammer())
> 
> in #routes.py
> routes_in=(('.*(php|PHP|asp|ASP|jsp|JSP)','/security/default/jam'),)
> 
> and it does not matter which web server you use.

Incidentally, here's how you do these kind of responses in WebOb.  The 
infinite response is not all that interesting, but you can do it like:

   resp = Response()
   chunk_size = 4096 # you could set this down to 1 to stream
                     # one character at a time
   resp.app_iter = itertools.repeat('x'*chunk_size)
   return resp

Kind of boring, though.  If you want to serve a resource and support 
If-Modified-Since, etc., you can do something like this, imagining you 
are serving an image from a database:

   resp = Response()
   resp.body = row.image_content # bytes
   resp.last_modified = row.edited # datetime object
   resp.content_type = row.content_type # string
   resp.conditional_response = True
   return resp

Then it will support If-Modified-Since and Range requests.  If you add 
an ETag, it will support If-None-Match as well.  Instead of setting 
resp.body, you could also set resp.app_iter to some iterator over the 
content.  If your app_iter object has a method app_iter_range it can 
also more efficiently do range requests (though it only involves WebOb 
reading though some unnecessary bytes if you don't have that method).

-- 
Ian Bicking : ianb at colorstudy.com : http://blog.ianbicking.org


More information about the Chicago mailing list