python - How to preprocess all calls? -


i use bottle.route() redirect http queries appropriate functions

import bottle  def hello():     return "hello"  def world():     return "world"  bottle.route('/hello', 'get', hello) bottle.route('/world', 'get', world) bottle.run() 

i add preprocessing each call, namely capacity act upon source ip (obtained via bottle.request.remote_addr). can specify 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 looks awkward.

is there way plug preprocessing function on global level? (so each call goes though it?)

i think can use bottle's plugin

doc here: 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() 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -