Search code examples
pythonweb-servicessoapsoappy

When using soappy SOAPServer, how do I read the request's headers?


I've got a Python webservice using SOAPpy. The webservice server is structured as shown below:

class myClass:
  def hello():
    return 'world'

if __name__ == "__main__":
  server = SOAPServer( ( 'localhost', 8888 ) )
  myObject = myClass()
  namespace = 'whatever::namespace'
  server.registerObject( myObject, namespace )
  server.serve_forever()

If a client calls the hello() method from my webservice, how can I read the headers, so I can start logging some information (for example: ip address) for debugging?


Solution

  • Do you want to do the logging in your hello method? Here's a minimal example that shows how to pass the SOAPContext information (which can give you some of this info) into the function/method call:

    from SOAPpy import *
    
    def hello(_SOAPContext = None):
        return "Your IP address is %s" % _SOAPContext.connection.getpeername()[0]
    
    if __name__ == "__main__":
      server = SOAPServer( ( '10.3.40.104', 8080 ) )
      server.registerFunction( MethodSig(hello, keywords=0, context=1) )
      server.serve_forever()