You Can Never Have Too Many Stock Tickers!

Updated July 17, 2002 | July 13, 2002 | Fredrik Lundh

Any site containing material on web services (dead link) isn’t really complete without a stock ticker example. So here’s a really simple one, using a SOAP service provided by and my little XML-over-HTTP helper class.

Let’s start with the code:

Example: Yet another stock ticker, sort of
from xmltoys import HTTPClient

# "Delayed Stock Quote" service from

service_uri = ""

action = "urn:xmethods-delayed-quotes#getQuote"

request = """\
<env:Envelope xmlns:env=""
<ns1:getQuote xmlns:ns1="urn:xmethods-delayed-quotes"
<symbol xsi:type="xsd:string">%s</symbol>

ticker_symbol = "LNUX"

client = HTTPClient.HTTPClient(service_uri)

response = client.do_request(
    request % ticker_symbol,
    extra_headers=[("SOAPAction", action)]

if 0:
    from xmltoys import ElementTree

for elem in response.getiterator("Result"):
    print elem.text

I’ve copied the SOAP action, a sample response body, and the host identifier directly from the method description available from the service provider (see their site for details).

You’ll need the xmltoys library and the http_xml helper to use this script. If you have, just run the script to see what it does.

Turning this into a real ticker is left as an exercise.


The SOAP request body contains an Envelope element, which contains a Body subelement holding the request. This is similar to XML-RPC’s methodCall and params elements.

To see what the server returns, change the if-statement before the ElementTree.dump call to “if 1:”, and run the script. This time, it will produce something like (slightly edited, for readability):


In this case, we can ignore the namespace stuff (ns prefixes and xmlns attributes) and the Element and Body elements as well; the return value we’re looking for is returned as the text content of an undecorated element called Result. The getiterator method returns an iterator matching all such elements, and we print the text attribute for the first one.

Note that you don’t really need to parse XML to find the response in this case. If you remove all tags, all that’s left is whitespace and the value we’re looking for. If we get the raw response instead of a tree, we can get rid of the junk with a simple RE substitution: re.sub(“<[^>]*>|\s+”, “”, response). Makes you wonder why they didn’t just put up a service that accepted a HTTP GET request, and returned the value as a text/plain response…

By the way, when I write this, the script prints 0.85. Your mileage may vary.



Today, it prints 0.66.

This example will be added to the next xmltoys release, as

The upcoming version of xmltoys can be used to build the SOAP request envelope. Here’s an outline (untested):

class NS:
    # helper to manufacture decorated tags
    def __init__(self, uri):
        self.uri = uri
    def __call__(self, tag):
        return "{%s}%s" % (self.uri, tag)

# standard namespaces and stuff
ns_env = NS("")
schema_instance = NS("")
schema_data = NS("")
soap_encoding_uri = ""

def marshal_request(method_uri, method_tag, params,
    envelope = Element(ns_env("Envelope"))
    body = SubElement(envelope, ns_env("Body"))
    method = SubElement(
        body, NS(method_uri)(method_tag),
        **{ns_env("encodingStyle"): encoding_uri}
    for param in params:
    return envelope

params = Element("") # container element

# FIXME: add type annotations (not required by this service)
SubElement(params, "symbol").text = ticker_symbol

request = marshal_request(method_uri, method_tag, params)

Move this to element-soap!

A Django site. rendered by a django application. hosted by webfaction.