Using the ElementTree Module to Generate SOAP Messages, Part 1: Talking to a Stock Quote Server

The Simple Object Access
Protocol (SOAP) is an XML-based protocol for “information exchange
in distributed environments”. SOAP can be used in many different ways,
but the most common approach is to use it for RPC requests over HTTP,
where a client application sends a SOAP request message to a remote
server, and the server returns a SOAP response message to the
client.

A simple SOAP request can look something like this (somewhat
simplified; namespace definitions not shown):

The above request refers to a method named this:method
(where the this prefix represents some namespace), and passes
in a single string argument. The server might return a SOAP response
looking something like:

A complete description of this service is available
as a WSDL file, which is a rather verbose XML format that contains
everything you’ll ever need to know about this SOAP service, in a
machine readable format. To make things a little bit easier, we’re
going to work from an “RPC profile” that’s available from
the
service description page:

Method Name

getQuote

Endpoint URL

http://66.28.98.121:9090/soap

SOAPAction

urn:xmethods-delayed-quotes#getQuote

Method Namespace URI

urn:xmethods-delayed-quotes

Input Parameters

symbol (string)

Output Parameters

Result (float)

Translated to english, this tells you that to issue a getQuote
request, you need to send an HTTP request to the given endpoint URL,
include a SOAPAction field in the HTTP request header, and provide
a request body named {urn:xmethods-delayed-quotes}getQuote
which contains a single input parameter, symbol. If
successful, the server will return a response body containing a
Result value.

Building SOAP requests with the Element module is straightforward.
Let’s start with some definitions and helper functions:

The SoapRequest function creates a SOAP request element from
the full method name (method namespace plus method name). The
SoapElement function adds a typed element to a parent
element. Note the use of the QName element class; this is used
to tell the ElementTree module that the attribute value contains
an XML namespace.

The next step is to write some glue code for the quote service.
The following class handles calls to the getQuote method, and
builds a request body which it passes to the call method on the
parent class:

The call method wraps the request element in SOAP Envelope
and Body elements, and uses the HTTPClient library to send the
request to the server. The method then extracts the response element from
the returned body, and returns it to the calling application.

Finally, here’s a snippet that uses the QuoteService to
fetch the current stock price for Red
Hat (LNUX):