简体   繁体   English

如何预处理所有呼叫?

[英]How to preprocess all calls?

I use bottle.route() to redirect HTTP queries to appropriate functions 我使用bottle.route()将HTTP查询重定向到适当的功能

import bottle

def hello():
    return "hello"

def world():
    return "world"

bottle.route('/hello', 'GET', hello)
bottle.route('/world', 'GET', world)
bottle.run()

I would like to add some preprocessing to each call, namely the capacity to act upon the source IP (obtained via bottle.request.remote_addr ). 我想为每个调用添加一些预处理,即对源IP进行操作的能力(通过bottle.request.remote_addr获得)。 I can specify the preprocessing in each route 我可以在每条路线中指定预处理

import bottle

def hello():
    preprocessing()
    return "hello"

def world():
    preprocessing()
    return "world"

def preprocessing():
    print("preprocessing {ip}".format(ip=bottle.request.remote_addr))

bottle.route('/hello', 'GET', hello)
bottle.route('/world', 'GET', world)
bottle.run()

but this looks awkward. 但这看起来很尴尬。

Is there a way to plug the preprocessing function on a global level? 有没有办法在全局级别上插入预处理功能? (so that each call goes though it?) (以便每个电话都能通过吗?)

I think you can use bottle's Plugin 我认为您可以使用Bottle的插件

doc here: http://bottlepy.org/docs/dev/plugindev.html#bottle.Plugin 此处的文档: http : //bottlepy.org/docs/dev/plugindev.html#bottle.Plugin

code example 代码示例

import bottle

def preprocessing(func):
    def inner_func(*args, **kwargs):
        print("preprocessing {ip}".format(ip=bottle.request.remote_addr))
        return func(*args, **kwargs)
    return inner_func

bottle.install(preprocessing)

def hello():
    return "hello"


def world():
    return "world"

bottle.route('/hello', 'GET', hello)
bottle.route('/world', 'GET', world)
bottle.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何优化预处理所有文本文档而不使用for循环在每次迭代中预处理单个文本文档? - How to optimize preprocess all text documents without using for loop to preprocess a single text document in each iteration? 如何为Tesseract预处理此图像? - How to preprocess this image for Tesseract? 当我有太多但需要全部数据时,如何预处理我的数据? - How do I preprocess my data when I have too much but need it all? 如何预处理传递给CreateView的值 - How to preprocess values passed to CreateView tensorflow中的MobileNet预处理输入如何 - How is the MobileNet preprocess input in tensorflow 如何对分类数据和数据框进行预处理 - How to preprocess with categorical data and dataframes 如何在Python中预处理时间序列数据以进行预测 - How to preprocess time series data in Python for forecasting 如何在列中的列表中添加一个字符串然后对其进行预处理? - How to add a string in list in column and then preprocess it? 如何使用python预处理Twitter文本数据 - How to preprocess twitter text data using python 如何预处理音频数据以输入到神经网络 - How to preprocess audio data for input into a Neural Network
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM