[mod_python] Publisher and DEFLATE Filter

Graham Dumpleton grahamd at dscpl.com.au
Sat Dec 2 18:08:15 EST 2006


On 03/12/2006, at 5:12 AM, Clodoaldo wrote:

> I want all the Publisher produced pages to be Apache DEFLATEd. So i
> added this directive to the virtual server configuration.
>
>   AddOutputFilter DEFLATE py
>
> As the Publisher does not require a py extension this obviously does
> not work. The AddOutputFilterByType directive is said to be deprecated
> in the 2.2.3 manual. So i add req.add_output_filter('DEFLATE') to the
> script and it works. But it will have to be added to all the scripts.
> Is there a way to set the DEFLATE filter to all Publisher scripts?
>
> If i use "SetOutputFilter DEFLATE" it will deflate everything
> including images and i will have to somehow disable it for the files
> or directories i eventually don't want to be deflated.
>
> I looked at the PythonOutputFilter but to activate it the
> AddOutputFilter directive must be used and it is back to the original
> problem. Or do I understand it wrong?

Presuming you are using something like:

   SetHandler mod_python
   PythonHandler mod_python.publisher

Instead using something like:

   SetHandler mod_python
   PythonHandler ~/_filters.py
   PythonHandler mod_python.publisher

Then in '_filters.py' in the handler root directory have:

   from mod_python import apache

   def handler(req):
     req.add_output_filter('DEFLATE')
     return apache.OK

This is making use of the ability to stack handlers, ie., have more than
one executed for the phase.

You could also have written:

   SetHandler mod_python
   PythonHandler ~/_publisher.py

Then in '_publisher.py' in the handler root directory have:

   from mod_python import publisher

   def handler(req):
     req.add_output_filter('DEFLATE')
     return publisher.handler(req)

I have used an underscore prefix for the file adding the filter as  
then publisher
will ignore it and not allow requests against it.

Note for others, this requires mod_python 3.3 as it is only in 3.3  
which the
ability to add output filters dynamically was added. Knowing it was  
3.3, I have
used the path method for referencing a module in the handler directive,
which is another new 3.3 feature.

BTW, using a wrapper like '_publisher.py' is also one way of managing  
the
creation of user sessions without having to add the session code in  
every
handler. For example:

   from mod_python import publisher, Session

   def handler(req):
     req.add_output_filter('DEFLATE')
     req.session = Session.Session(req)
     if req.session.is_new():
       ...
     ...
     return publisher.handler(req)

For more complicated session stuff requiring login and which needs to  
control
access to static files or other types of handlers, you are much  
better off using
an authenhandler(), something which is much easier to do properly in  
3.3.

Graham


More information about the Mod_python mailing list