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


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)

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:')


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!

HEADERS: {'SOAPAction': u'""', 'Content-Type': 'text/xml; charset=utf-8', 'Content-type': 'text/xml; charset=utf-8', 'Soapaction': u'""'}
<SOAP-ENV:Envelope xmlns:ns0="" xmlns:ns1="" xmlns:xsi="" xmlns:SOAP-ENV="">
         <Mailbox xmlns="">
Posted in Uncategorized | Tagged , , , | Leave a comment

Leave a Reply