How can I add a hook before and after processing a request on twisted.web (twisted.web2 is fine too)? The equivalent of webpy's:
如何在twisted.web上处理请求之前和之后添加一个钩子(twisted.web2也可以)?相当于webpy的:
app = web.application(urls, globals())
app.add_processor(web.loadhook(my_attach_callback))
app.add_processor(web.unloadhook(my_detach_callback))
Thanks!
1 个解决方案
#1
One approach Twisted Web allows is to insert an extra resource into the resource hierarchy the only purpose of which is to run your custom hooks, rather than to actually handle a segment of the request URL as resources typically do.
Twisted Web允许的一种方法是在资源层次结构中插入额外的资源,其唯一目的是运行自定义挂钩,而不是像资源通常那样实际处理请求URL的一部分。
You can find an implementation of this approach in twisted/web/_auth/wrapper.py which implements the HTTPAuthSessionWrapper resource (exposed publicly in twisted.web.guard). Note the first line of getChildWithDefault
which ensures the resource doesn't consume one of the request segments. This allows it to sit in the resource hierarchy, modify behavior, but not otherwise change the way URLs are dispatched.
你可以在twisted / web / _auth / wrapper.py中找到这种方法的实现,它实现了HTTPAuthSessionWrapper资源(在twisted.web.guard中公开公开)。请注意getChildWithDefault的第一行,它确保资源不使用其中一个请求段。这允许它位于资源层次结构中,修改行为,但不会改变URL分派的方式。
#1
One approach Twisted Web allows is to insert an extra resource into the resource hierarchy the only purpose of which is to run your custom hooks, rather than to actually handle a segment of the request URL as resources typically do.
Twisted Web允许的一种方法是在资源层次结构中插入额外的资源,其唯一目的是运行自定义挂钩,而不是像资源通常那样实际处理请求URL的一部分。
You can find an implementation of this approach in twisted/web/_auth/wrapper.py which implements the HTTPAuthSessionWrapper resource (exposed publicly in twisted.web.guard). Note the first line of getChildWithDefault
which ensures the resource doesn't consume one of the request segments. This allows it to sit in the resource hierarchy, modify behavior, but not otherwise change the way URLs are dispatched.
你可以在twisted / web / _auth / wrapper.py中找到这种方法的实现,它实现了HTTPAuthSessionWrapper资源(在twisted.web.guard中公开公开)。请注意getChildWithDefault的第一行,它确保资源不使用其中一个请求段。这允许它位于资源层次结构中,修改行为,但不会改变URL分派的方式。