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

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

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -