librelist archives

« back to archive

Gzip compression

Gzip compression

From:
Davide Muzzarelli
Date:
2010-06-14 @ 14:40
Hi,
I'm writing an extention for add the gzip compression feature.

In the code I have to see if, in the request, the browser is compatible and 
compress the response.

I set up a class like this:

import cStringIO, gzip

class Gzip(object):
    def __init__(self, app, compress_level=6):
        self.app = app
        self.compress_level = compress_level
        
        self.app.after_request(self.after_request)

    def after_request(self, response):
        # code for verify the browser header in the request
        pass # I have not the request!!!
        
        # Compress the response
        if response.status_code != 200 or len(response.data) < 500 \
        or 'Content-Encoding' in response.headers:
            return response
        
        gzip_buffer = cStringIO.StringIO()
        gzip_file = gzip.GzipFile(mode='wb', \
        compresslevel=self.compress_level, fileobj=gzip_buffer)
        gzip_file.write(response.data)
        gzip_file.close()
        response.data = gzip_buffer.getvalue()
        response.headers['Content-Encoding'] = 'gzip'
        response.headers['Content-Length'] = str(len(response.data))
        return response

The problem is that I have not the request! Where I can find it?

---
Davide Muzzarelli
www.dav-muz.net

Re: [flask] Gzip compression

From:
Armin Ronacher
Date:
2010-06-14 @ 14:47
Hi,

On 2010-06-14 4:40 PM, Davide Muzzarelli wrote:
> The problem is that I have not the request! Where I can find it?
from flask import request, then you can access it from anywhere.


Regards,
Armin

Re: [flask] Gzip compression

From:
Davide Muzzarelli
Date:
2010-06-14 @ 14:52
In data lunedì 14 giugno 2010 16:47:58, Armin Ronacher ha scritto:
> from flask import request, then you can access it from anywhere.

Just importing this? Easy! :)

Is it thread safe (if generate problems when there are many requests per 
second)?

---
Davide Muzzarelli
www.dav-muz.net

Re: [flask] Gzip compression

From:
Armin Ronacher
Date:
2010-06-14 @ 14:55
On 2010-06-14 4:52 PM, Davide Muzzarelli wrote:
> Is it thread safe (if generate problems when there are many requests per
> second)?
Yes.  This is thread safe:
   http://flask.pocoo.org/docs/quickstart/#context-locals


Regards,
Armin

Re: [flask] Gzip compression

From:
Davide Muzzarelli
Date:
2010-06-14 @ 15:13
In data lunedì 14 giugno 2010 16:55:38, Armin Ronacher ha scritto:
> Yes.  This is thread safe:
>    http://flask.pocoo.org/docs/quickstart/#context-locals

Thanks!

---
Davide Muzzarelli
www.dav-muz.net

Re: [flask] Gzip compression

From:
Kenneth Reitz
Date:
2010-06-14 @ 14:42
I personally believe that gzip compression should occur on the server 
(e.g. apache), not the application. 

Kenneth Reitz
http://kennethreitz.com/contact-me

On Jun 14, 2010, at 10:40 AM, Davide Muzzarelli wrote:

> Hi,
> I'm writing an extention for add the gzip compression feature.
> 
> In the code I have to see if, in the request, the browser is compatible and 
> compress the response.
> 
> I set up a class like this:
> 
> import cStringIO, gzip
> 
> class Gzip(object):
>    def __init__(self, app, compress_level=6):
>        self.app = app
>        self.compress_level = compress_level
> 
>        self.app.after_request(self.after_request)
> 
>    def after_request(self, response):
>        # code for verify the browser header in the request
>        pass # I have not the request!!!
> 
>        # Compress the response
>        if response.status_code != 200 or len(response.data) < 500 \
>        or 'Content-Encoding' in response.headers:
>            return response
> 
>        gzip_buffer = cStringIO.StringIO()
>        gzip_file = gzip.GzipFile(mode='wb', \
>        compresslevel=self.compress_level, fileobj=gzip_buffer)
>        gzip_file.write(response.data)
>        gzip_file.close()
>        response.data = gzip_buffer.getvalue()
>        response.headers['Content-Encoding'] = 'gzip'
>        response.headers['Content-Length'] = str(len(response.data))
>        return response
> 
> The problem is that I have not the request! Where I can find it?
> 
> ---
> Davide Muzzarelli
> www.dav-muz.net

Re: [flask] Gzip compression

From:
Armin Ronacher
Date:
2010-06-14 @ 14:48
On 2010-06-14 4:42 PM, Kenneth Reitz wrote:
> I personally believe that gzip compression should occur on
 > the server (e.g. apache), not the application.
this.


Regards,
Armin

Re: [flask] Gzip compression

From:
Davide Muzzarelli
Date:
2010-06-14 @ 15:11
In data lunedì 14 giugno 2010 16:42:19, Kenneth Reitz ha scritto:
> I personally believe that gzip compression should occur on the server (e.g.
> apache), not the application.

Thank for your reply Kenneth, but there are servers or hostings that haven't 
it.

In this case I wrote it for learn Flesk and Werkzeug, I may will write the 
same for Werkzeug.

---
Davide Muzzarelli
www.dav-muz.net