[mod_python] Output Filters Redeaux

Graham Dumpleton grahamd at dscpl.com.au
Mon Apr 17 17:34:29 EDT 2006


BTW, since you are modifying the length of the content, you must  
ensure you
delete any content length header in the req.headers_out, otherwise it  
will
disagree with what is actually sent and clients may hang waiting for  
data or
prematurely truncate data.

The deletion of the header must be done before the first call to  
filter.write().

On 18/04/2006, at 2:36 AM, Lee Brown wrote:

> Greetings!
>
> This is the code for a Mod Python output filter that performs an  
> XSLT transform on XML data that I published a few weeks ago.  I've  
> restructured it as an example application of the filter template:
>
> from mod_python import apache
> from cStringIO import StringIO
> import lxml.etree
> import time
>
> name = 'LXML'
> timestring = 'Time to process XML/XSLT files using %s: %.3f  
> milliseconds (%s pages per second) [%s pass]\n'
>
> xsltfile = open('c:/webdev/sites/crashtest/templates/ 
> template.xslt', 'rU')
> styledoc = lxml.etree.parse(StringIO(xsltfile.read()))
> transformer = lxml.etree.XSLT(styledoc)
> xsltfile.close()
>
> def outputfilter (filter):
>     try:
>         streambuffer = filter.req.streambuffer
>     except AttributeError:

            try:
              del filter.req.headers_out["Content-Length"]
            except:
              pass

>         filter.req.streambuffer = StringIO()
>         streambuffer = filter.req.streambuffer
>         filter.req.start = time.clock()
>         filter.req.passes = 0
>
>     streamlet = filter.read()
>     while streamlet:
>         filter.req.passes += 1
>         streambuffer.write(streamlet.replace('\r\n', '\n'))
>         streamlet = filter.read()
>
>     if streamlet is None:
>         doc = lxml.etree.parse(streambuffer)
>         doc.xinclude()
>         result = str(transformer(doc))
>         end = time.clock()
>         start = filter.req.start
>         passes = filter.req.passes
>         ms = (end-start)*1000
>         pps = int(1/(end-start))
>         timestamp = timestring % (name, ms, pps, passes)
>         filter.write(result.replace('benchmark', timestamp))
>         filter.close()
>
>
>
> Best Regards,
> Lee E. Brown
> (leebrown at leebrown.org)
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list