Published on (
 See this if you're having trouble printing code examples

Using PySOAP

by Cameron Laird

You don't have to become expert in XML to use SOAP. For most programmers, SOAP is just a technique for distributed computing. You write a function call on your computer here, and it retrieves a result from a computation there, on a server perhaps half a world away.

With well under an hour of installation and practice, you can learn to write distributed Python programs using SOAP to exchange services and results from programs written in almost any language.

Compared to other distributed programming tools like DCOM or CORBA (or, more properly, the IIOP which transports CORBA,) SOAP is

For example, you might use SOAP and Python to write an application for your handheld computer that retrieves data from a mainframe using Cobol. SOAP translates the details of computing language, hardware, and even human-language encoding. Russian-language words appear in proper Cyrillic, and so on.

A growing number of Python programmers are using SOAP. ActiveState Tools Corporation provides product updates using SOAP as a vehicle. Digital Creations Inc.'s Zope is SOAP-enabled. With just a little setup time, you too can experience SOAP's benefits.

Starting with SOAP

Programming with SOAP is easy, but first we must assemble the proper pieces to make SOAP work. In principle, you can use Python 2.0 or later. However, PySOAP depends on the xml.parsers.expat module, which is difficult to install correctly. Installing the Windows binary for Python2.1 final is the easiest way to get PySOAP running. If you work from sources, you'll need expat 1.1 or later.

Once you are able to

import xml.parsers.expat

you're ready to download PySOAP from Sourceforge. For this tutorial, I used version 0.9.5. Unpack it into a local directory, and copy the *.py sources into a standard library location as appropriate. On a Unix-like machine, for example, you might

cp SOAPpy095/ /usr/local/lib/python2.0/

This makes SOAP available to all Python2.0 developers on your host. Under Windows you'd want

copy SOAPpy095\ \python2.1\lib

Now you're ready to exercise SOAP. Run

import SOAP
  # provides several interesting SOAP
  # services.  See the references below for details.
server = SOAP.SOAPProxy(
  namespace = "urn:xmethods-Temperature")
  # 47978 is Rensselaer's zip (postal) code.
print server.getTemp("47978")

This returns the temperature in Fahrenheit degrees of a small town in northwest Indiana, in the US Mid-West. You have just begun your career as a SOAP programmer.

Now What do I do With It?

There are two approaches you can take to your SOAP education. One is to regard SOAP simply as a featureful, remote-procedure mechanism. It's useful in that role. Many organizations are rolling out internal applications that rely on SOAP as a public programming interface. SOAP's portability plays nicely in this game. It's convenient to write servers in systems programming languages including Java and extended SQL while rapidly developing clients in languages like Visual Basic, Python, and Delphi. You can also use PySOAP to write servers in Python with as little code as

import SOAP

def echo(s):
  return s + "\n  " + s

server = SOAP.SOAPServer(("localhost", 8080))

You'd access this in Python with

import SOAP 

server = SOAP.SOAPProxy("http://localhost:8080")
print server.echo("Hello, there ...")

Notice what the SOAP module achieves: echo, is defined on the server side but is exposed as a method available for use in the client. The PySOAP distribution includes a SOAPpy095/README with salient examples of client and server use.

Public SOAP

You can also use SOAP to communicate across an organizational boundary. In this model, SOAP is part of a larger, emerging standard called Web Services. Web Services provide computer-to-computer information and communications similar to the way the Web currently mediates between computers and humans. Think of it this way: the Weather Channel delivers pages humans can read, with information about temperature and other conditions. The XMethods service demonstrated above renders similar information, in a form that's handy for further automated computations.

If you're a public utility, for example, you might use public Web Services for temperature, humidity, fuel prices, and other variables to calculate improved management of power generation and distribution.


One of the best-documented Web Services is the Meerkat syndication service. Meerkat uses XML-RPC to communicate content and information about content. How does SOAP compare to XML-RPC? Which should you use?

As of June 2001, XML-RPC remains more mature and simpler to implement. However, there are several reasons I favor SOAP for my own development work. XML-RPC's easier implementation matters little to me in application programming because PySOAP wraps up SOAP's difficulty. With a good SOAP binding in hand, I find SOAP's scalability, extensibility, transaction-awareness, and transport flexibility make it easier to use than XML-RPC for typical problems. XML-RPC is only a remote-procedure technology, with a fixed set of data types and XML element names. SOAP adds to these the ability to marshal objects (in the sense of an object-oriented language like Python) and messages, and it can also communicate named parameters and attributes.

As convenient as I find PySOAP, I exaggerated when I wrote, "PySOAP wraps up SOAP's difficulty." The SOAP specification defines many variations and options, only a fraction of which PySOAP 0.95 implements. It's the most important fraction, though, and even the 0.95 release is an adequate base for plenty of useful development.

If you're a consumer of a public SOAP service, like one of those listed on XMethods, you'll need to use SOAP rather than XML-RPC. Moreover, industry heavyweights including IBM and Microsoft have sent clear signals that they've decided SOAP is their long-term preference as a remote-procedure protocol.

You can learn more about PySOAP and related matters through th python webservices mailing list or by following the pointers I maintain on my web site..

Cameron Laird is the vice president of Phaseit, Inc. and frequently writes for the O'Reilly Network and other publications.

Return to the Python DevCenter.

Copyright © 2009 O'Reilly Media, Inc.