Using logging to debug what suds is sending across the wire

suds, a SOAP client for Python, makes good use of Python’s logging module, which makes it easy to work out what it’s sending across the wire.

I’d previously mentioned hacking suds to use an urllib2 handler with debugging enabled. Modifying libraries systemwide, while it works, isn’t the prettiest way to work out what’s happening, and so we should turn to the logging framework.

The logging module contains a basicConfig method, which we could invoke thus:

import logging

logging.basicConfig(level=logging.DEBUG)

However, suds chucks out a log of stuff at this level, the majority of which we’re not interested in. From observing this (or reading the code) we note that we’re really only interested in the suds.transport.http logger. Hence we attach a handler to just this logger:

import logging

handler = logging.StreamHandler(sys.stderr)
logger = logging.getLogger('suds.transport.http')
logger.setLevel(logging.DEBUG), handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

Furthermore, if we’re only interested in outgoing messages, we can attach a filter:

class OutgoingFilter(logging.Filter):
    def filter(self, record):
        return record.msg.startswith('sending:')

handler.addFilter(OutgoingFilter())

We now get lots of messages like the following spewed out on stderr, perfect for working out whether your code is constructing the right SOAP requests!

sending:
URL:https://nexus.ox.ac.uk/EWS/exchange.asmx
HEADERS: {'SOAPAction': u'"http://schemas.microsoft.com/exchange/services/2006/messages/GetUserOofSettings"', 'Content-Type': 'text/xml; charset=utf-8', 'Content-type': 'text/xml; charset=utf-8', 'Soapaction': u'"http://schemas.microsoft.com/exchange/services/2006/messages/GetUserOofSettings"'}
MESSAGE:
<SOAP-ENV:Envelope xmlns:ns0="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:ns1="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns1:GetUserOofSettingsRequest>
         <Mailbox xmlns="http://schemas.microsoft.com/exchange/services/2006/types">
            <ns0:Address>firstname.lastname@unit.ox.ac.uk</ns0:Address>
         </Mailbox>
      </ns1:GetUserOofSettingsRequest>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Posted in Uncategorized | Tagged , , , | Leave a comment

Comments are closed.