开发者

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

开发者 https://www.devze.com 2023-01-10 16:32 出处:网络
I\'ve got a Python webservice using SOAPpy.The webservice server is structured as shown below: class myClass:

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 m开发者_StackOverflow社区y webservice, how can I read the headers, so I can start logging some information (for example: ip address) for debugging?


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()


you can add a RequestHandler to the SoapServer object that extends the SOAPRequestHandler and replaces the HeaderHandler (have a look at the source of server.py for an example)

0

精彩评论

暂无评论...
验证码 换一张
取 消