瓶装预请求处理

2024-10-02 00:39:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我试图这样做,这样我就不会重复代码,但无法在BottlyPy中找到这样的方法。。。在

我的设置是:nginx->;uwsgi->;battlepy。在


Tags: 方法函数代码gt路由foorequestenviron
1条回答
网友
1楼 · 发布于 2024-10-02 00:39:37

这就是plugins的用途。在

下面是一个例子:

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)

相关问题 更多 >

    热门问题