[Chicago] help from Django and Pylons developers

Massimo Di Pierro mdipierro at cs.depaul.edu
Mon Apr 28 23:37:09 CEST 2008


It is not interesting but very useful. they do not port scan me anymore!
In web2py if you just do

def index(): return response.stream 
('somefile.flv',chunk_file=40000,request=request)

If will check request, determine if to do a IF_MODIFIED_SINCE and  
returns 304
determines if is a range request and returns a 206 PARTIAL CONTENT
if the file is larger than 40000 will stream (the required range) in  
chunks of that size.
If will also set the proper response headers including CONTENT_TYPE.
If the files does not exist returns the 400.

Massimo



On Apr 28, 2008, at 4:11 PM, Ian Bicking wrote:

> 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
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago



More information about the Chicago mailing list