0

我正在寻找一种foo()在进入路由之前让所有请求进入函数内部的方法。

这样我就可以request.environ在做真正的工作之前阅读。

我正在尝试这样做,以免重复代码,但找不到在BottlyPy中执行此类操作的方法...

我的设置是:nginx -> uwsgi -> bottlepy。

4

1 回答 1

3

这就是插件的用途。

这是一个例子:

import bottle
from bottle import request, response

def foo(callback):
    def wrapper(*args, **kwargs):
        # before view function execution
        print(request.environ)  # do whatever you want

        body = callback(*args, **kwargs)  # this line basically means "call the view normally"

        # after view function execution
        response.headers['X-Foo'] = 'Bar'  # you don't need this, just an example

        return body  # another 'mandatory' line: return what the view returned (you can change it too)
    return wrapper

bottle.install(foo)
于 2012-08-21T16:00:19.333 回答