Chapter 21
SOAP
XML-RPC solves REST’s main problem by defining a standard way to represent data types such as integers, dates, and lists. However, while XML-RPC was being defined, the W3C’s XML working group was working on its own representation of those data types and many others. After XML-RPC became popular, the W3C turned their attention to it, and started redesigning it to use their preexisting standards.
Along the way, ambition broadened the scope of their project to include any kind of message exchange, not just procedure calls and their return values. The result was SOAP. The acronym originally stood for Simple Object Access Protocol, but because the standard’s scope has been expanded so far beyond simple remote procedure calls, the acronym itself is no longer applicable.
SOAP may still be simple compared to COM or CORBA, but it’s a lot more complicated than XML-RPC. Fortunately, you don’t need all of SOAP just to expose a web application as a web service. The part you do need looks basically like XML-RPC with a more general XML encoding scheme. SOAP gives you access to a broader range of data types than XML-RPC, and it lets you define your own, but this introductory treatment will just treat it as an analogue of XML-RPC.
SOAP Quick Start: Surfing the Google API
Just as with REST and XML-RPC, a SOAP message is typically sent as the data portion of an HTTP POST request. Just as with those other protocols, then, it’s technically possible to use a SOAP web service without any SOAP-specific tools: Just construct the message by hand, send it off with urllib, and parse the response with the xml.sax module. Realistically, though, you need a SOAP library to use SOAP with Python. A SOAP library will deal with transforming Python data structures to SOAP’s XML representations and back, just as xmlrpclib does for XML-RPC.
Unfortunately, there’s no “soaplib” bundled with Python, but you can download one. There are two SOAP libraries for Python. The one library we’ll use in this chapter is SOAPpy, which provides an xmlrpclib-like version of a SOAP client and a SOAP server.
If you’re running Debian GNU/Linux, you can just install the “soappy” package; if not, you can download the distribution from http://pywebsvcs.sourceforge.net/. ZSI, the other Python SOAP package, is also available from that site. Be warned that SOAPpy requires two other packages: fpconst, a floating-point library, and PyXML, a set of XML utilities. More information and links to the packages are available in the SOAPpy README file.
To start using SOAPpy, you’ll need a SOAP service to call. As it happens, Google Web Services, one of the most celebrated web service APIs, uses SOAP. Google’s SOAP API gives you access to the data set used by their search engine.
To use the APIs, you’ll need to sign up with Google for an API key, just as with Amazon in the REST example. Also as with Amazon, Google puts limitations on your use of their API. Whereas Amazon allows only one API call per second per IP address, Google limits you to one thousand API calls per day per API key. Be careful while experimenting with this service; a script that goes out of control could leave you unable to make any more API calls until the next day.
Sign up for a Google API key at http://api.google.com/createkey.