Sugath Mudali's Blog

May 29, 2012

Log SOAP request / response in python

Filed under: python — sugath @ 12:36 am

First step is to install suds, a lightweight SOAP python client for consuming Web Services

1. Install python setuptools.py and run ez_setup.py
2. Download suds and run setup.py to install
3. Test the client

>>> url = 'http://www.webservicex.net/stockquote.asmx?WSDL'
>>> client = Client(url)
>>> print client

Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( StockQuote ) tns="http://www.webserviceX.NET/"
   Prefixes (0)
   Ports (2):
      (StockQuoteSoap)
         Methods (1):
            GetQuote(xs:string symbol, )
         Types (0):
      (StockQuoteSoap12)
         Methods (1):
            GetQuote(xs:string symbol, )
         Types (0):
>>> client.service.GetQuote('GOOG')

Output:

<StockQuotes>
  <Stock>
    <Symbol>GOOG</Symbol>
    <Last>641.24</Last>
    <Date>3/30/2012</Date>
    <Time>4:00pm</Time>
    <Change>-7.17</Change>
    <Open>651.75</Open>
    <High>653.49</High>
    <Low>641.00</Low>
    <Volume>2312245</Volume>
    <MktCap>208.5B</MktCap>
    <PreviousClose>648.41</PreviousClose>
    <PercentageChange>-1.11%</PercentageChange>
    <AnnRange>473.02 - 670.25</AnnRange>
    <Earns>29.76</Earns>
    <P-E>21.79</P-E>
    <Name>Google Inc.</Name>
  </Stock>
</StockQuotes>

4. Add logging

>>> import logging
>>> logging.basicConfig(level=logging.INFO)
>>> logging.getLogger('suds.client').setLevel(logging.DEBUG)
>>> client.service.GetQuote('GOOG')

Output:

DEBUG:suds.client:sending to (http://www.webservicex.net/stockquote.asmx)
message:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:ns0="http://www.webserviceX.NET/"
xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns1:Body>
      <ns0:GetQuote>
         <ns0:symbol>GOOG</ns0:symbol>
      </ns0:GetQuote>
   </ns1:Body>
</SOAP-ENV:Envelope>
DEBUG:suds.client:headers = {'SOAPAction': u'"http://www.webserviceX.NET/GetQuote"',
'Content-Type': 'text/xml; charset=utf-8'}
DEBUG:suds.client:http succeeded:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetQuoteResponse xmlns="http://www.webserviceX.NET/">
      <GetQuoteResult>
        <StockQuotes>
          <Stock>
            <Symbol>GOOG</Symbol>
            <Last>641.24</Last>
            <Date>3/30/2012</Date>
            <Time>4:00pm</Time>
            <Change>-7.17</Change>
            <Open>651.75</Open>
            <High>653.49</High>
            <Low>641.00</Low>
            <Volume>2312245</Volume>
            <MktCap>208.5B</MktCap>
            <PreviousClose>648.41</PreviousClose>
            <PercentageChange>-1.11%</PercentageChange>
            <AnnRange>473.02 - 670.25</AnnRange>
            <Earns>29.76</Earns>
            <P-E>21.79</P-E>
            <Name>Google Inc.</Name>
          </Stock>
        </StockQuotes>
      </GetQuoteResult>
    </GetQuoteResponse>
  </soap:Body>
</soap:Envelope>
<StockQuotes>...</StockQuotes>

Please note HTML entities were replaced in GetquoteResponse for readability

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.