Python web service call logger

I wanted to log every access to my web services (using soaplib): Get the client IP, what method is it calling, what parameters, and what class. Rather than write the code to log the access in every method I've written a decorator (logWebServiceCall) so you only need to add it on the top of your web method, like this:

  2. class MyService(SimpleWSGISoapApp )
  3. @logWebServiceCall
  4. @soapmethod(String, String, _returns=Array(String))
  5. def myMethod(self, param1, param2):
  6. ...code here...

Decorator code:

  2. from soaplib.wsgi_soap import request
  4. def logWebServiceCall(func):
  5. def wrapped(*args, **kwargs):
  6. if hasattr(request, "environ"):
  7. if not kwargs.has_key('_soap_descriptor'):
  8. message = request.environ['SCRIPT_NAME']
  9. message += " - " + request.environ['HTTP_SOAPACTION']
  10. message += " - " + request.environ['REMOTE_ADDR']
  11. message += " - " + str(list(args)[1:])
  12. print message
  13. result = func(*args, **kwargs)
  14. return result
  15. wrapped.__doc__ = func.__doc__
  16. wrapped.func_name = func.func_name
  17. wrapped._is_soap_method = True
  18. return wrapped

Just finished a decorator for validation. Same usage than the previous one.

  2. def validateUserFirst(func):
  3. def wrapped(*args, **kwargs):
  4. if hasattr(request, "environ"):
  5. if not kwargs.has_key('_soap_descriptor'):
  6. argumentsList = list(args)[1:]
  7. if len(argumentsList) < 2:
  8. raise Exception('This method requires username and password at least')
  10. username = argumentsList[0]
  11. password = argumentsList[1]
  13. if not validate(username, password): # define the validation method as you want
  14. raise Exception('Invalid username or password')
  16. result = func(*args, **kwargs)
  17. return result
  18. wrapped.__doc__ = func.__doc__
  19. wrapped.func_name = func.func_name
  20. wrapped._is_soap_method = True
  21. return wrapped
  • esauro

    Using decorators for such purposes is a great idea.

    I think you should take a look into logging module [1]. It provides differents log levels (debug, warn, error, etc) as well as a single way to log into a lot of medium (stream, file, socket, smtp, etc). You can log to all those places just modifiying a parameter.


  • Carlos Ble

    Thanks Esau 🙂
    This technic of using decorators is part of the Aspect Oriented Programming paradigm.
    Thanks for the link, I’ll definitively use that module to log.

  • big bang theory season 1

    A colleague of mine asked me to check out your website three weeks ago, since us two enjoy the same stuff (yeah we’re close buddies, clone heads, been 16 years) and I seriously got to say that I am greatly impressed. Me and my friend were actually considering to start a similar site just like yours, but we have had some struggles. First, neither of us is a technology individual, 2ndly, we do not really know where to start. I know alot of people managed to get their Dot Coms on the net just recently (due to cheap prices, etc) but both of us are still having struggles. I would really appreciate it if you recommend us a place to start from, because we don’t have a clue LOL. Yes it sucks being 48 year old internet literates… or should I say dotcom literates?

  • Manish Lamrod

    Magnificent beat ! I would like to apprentice at the same time as you amend your site, how could i subscribe for a blog web site? The account helped me a appropriate deal. I had been a little bit familiar of this your broadcast offered bright clear concept

  • FB Optin App Review

    Hi-Ya! our family just loves your terrific editorial thanx and please stick to it

  • NorClorse

    Nuariusbart – cheap tom s shoes

  • NorClorse

    Nuariusbart – toms sale

  • Lester