webpy_processor_hook

 

processor

# some processing before and after executing the requests

Example:

#!/usr/bin/env python

import web

urls = (
    '/', 'index'
)

class index:
    def GET(self):
        print "in handling"
        return "testing"

def myprocessor(handler):
    print 'before handling'
    result = handler()
    print 'after handling'
    return result

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.add_processor(myprocessor)
    app.run()

 

output log:

before handling
in handling
after handling
192.168.123.21:50299 - - [24/Jan/2015 19:27:40] "HTTP/1.1 GET /" - 200 OK

 


hook

do actions at begining and end of requests

與 processor 不同在於可以用 web.ctx, web.header 及 web.input()

def my_loadhook():
    web.header('Content-type', "text/html; charset=utf-8")

def my_unloadhook():
    print "my unload hook"

app.add_processor(web.loadhook(my_loadhook))
app.add_processor(web.unloadhook(my_unloadhook))

 

 

 

Creative Commons license icon Creative Commons license icon