Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 预请求处理_Python_Uwsgi_Bottle - Fatal编程技术网

Python 预请求处理

Python 预请求处理,python,uwsgi,bottle,Python,Uwsgi,Bottle,我正在寻找一种方法,使所有请求在进入路由之前都进入函数foo() 这样我就可以在做真正的工作之前阅读request.environ 我正试图这样做,这样我就不会重复代码,但在BottlyPy中找不到一种方法来做这样的事情 我的设置是:nginx->uwsgi->bottlepy。这就是它们的用途 下面是一个例子: import bottle from bottle import request, response def foo(callback): def wrapper(*args

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

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

我正试图这样做,这样我就不会重复代码,但在BottlyPy中找不到一种方法来做这样的事情

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

这就是它们的用途

下面是一个例子:

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)
这就是它们的用途

下面是一个例子:

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)

你的意思是我应该写我自己的插件只是为了初始化一些我需要阅读request.environ的东西吗?没错。制作瓶子插件并不难。太棒了,非常感谢你提供的非常简单的代码示例。你是说我应该编写自己的插件来初始化一些我需要阅读请求的东西。环境?没错。制作瓶子插件并不难。太棒了,非常感谢您提供的非常简单的代码示例。