Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Web service clients with Python

In this tip, William Brogden discusses the growing popularity of the object-oriented scripting language, Python.

Python is an object-oriented scripting language with growing popularity. As with many language projects, what started...

as the creation of a single programmer, in this case Guido van Rossum, has evolved considerably. The language is now an open source project whose copyright is held by the non-profit Python Software Foundation. The license is similar to that used by the Apache Software Foundation. As of this writing, the current version is 2.4.3, available as a remarkably compact download as a Windows installer or Unix source code package. Since a Python distribution is so compact, many recent Linux distributions include Python.

About the name Python: Yes, the original author was under the influence of the British comedy troupe "Monty Python." This fun-loving attitude continues in the Python community. For example, one major directory of Python packages is known as the "Cheese Shop."

Although Python is easily used for simple scripts, it has also been used to build complex applications such as the "Zope" Web application server. Google uses Python for many backend Web services such as Google Maps. An active user community has contributed a large number of applications and toolkits to the public domain.

As a high level language, Python has been ported to many platforms. Java programmers may want to start with Jython, an open source Java-based implementation. With Jython you can add scripting style functions to your Java applications and use Java classes in Jython scripts.

The Python Programming Environment

Python source code creates both classes and function objects and is organized by modules. For programmers used to C-like languages such as Java, the biggest surprise will be the fact that the indentation of source code lines controls the extent of program blocks. Although this results in a consistent layout and high readability for Python code, new programmers must be constantly aware of this feature to avoid unexpected results.

Python provides the usual basic data types for numbers and strings. Unicode strings are a separate type from 8-bit character string, which constitutes somewhat of a problem when working with Web services and XML. It is likely that the next major revision of Python will switch entirely to Unicode.

The standard distribution contains a "Python's Integrated Development Environment" known as IDLE that uses the "Tk" graphic library to create a window with menu options. The programmer can use IDLE to edit Python source code or execute single Python statements. IDLE supports syntax coloring while editing code, debugging, method help and browsing of the class libraries.

Using Python XML Libraries

The standard Python distribution contains modules that provide for both Document Object Model (DOM) and Simple API for XML (SAX) parsing of XML documents. The xml.dom module supports the W3C's level 2 DOM specification. In other words you will see Node, Element and NodeList object references and node type constants just like you would in Java's JAXP library.

I found that the interactive scripting environment of IDLE is a great way to get familiar with Python and the XML libraries. Here is the sequence of statements I entered in the IDLE environment to parse Tomcat's default web.xml file and locate the "mime-mapping" elements. (Line numbers substituted for IDLE's prompt to faciltate discussion.)

  1. from xml.dom.minidom import parse
  2. import xml.dom
  3. dom = parse("c:\\Temp\web.xml")
  4. nlist = dom.getElementsByTagName('mime-mapping')
  5. print "There are ", nlist.length, " mime-mapping Elements"

Line 1 imports a single function from the xml.dom.minidom "lightweight dom implementation" module. Line 2 imports the entire xml.dom module which contains the DOM interfaces. Line 3 causes parsing of the web.xml file, creating a Document object and assigning the reference to the variable called "dom" - note that because Python is dynamically typed we don't have to declare a type for dom as we would in Java.

In line 4 we use the DOM method that returns a NodeList of all elements named "mime-mapping". As an example of how the IDLE environment facilitates learning the language, at this point I entered "dir( nlist )" and Python responded with all of the names of the parts of the nlist object. This revealed that there was a "length" member so finally in line 5 I could print the number of elements found, resulting in this output:

There are 145 mime-mapping Elements

The standard Python library also supports SAX style parsing of documents. The organization of the SAX-related packages will be very familiar to Java programmers. You can define a class implementing SAXcontentHandler and pass an instance to the SAX parser just like you would in Java.

Python as a Client for XML-RPC Services

The Python standard library contains the xmlrpclib module that supports writing XML-RPC clients using the standard XML-RPC data types. The following four lines entered in IDLE call a simple XML-RPC service and print the resulting string formatted in the ISO8601 style:

 import xmlrpclib server = xmlrpclib.ServerProxy("http://time.xmlrpc.com/RPC2" ) result = server.currentTime.getCurrentTime() print result

The Python standard library also includes classes that make it easy to create an XML-RPC based Web service.

Python as a SOAP Client

Support for SOAP in Python seems to be several years behind support in Java and Ruby. There are numerous projects and toolkits, but they are not integrated into the standard Python release. In addition, most of the links that Google and Yahoo turn up are several years old. As near as I can tell, the package with the most support is the Zolera SOAP Infrastructure (ZSI), which can be downloaded from the Web Services for Python project at SourceForge. This package includes tools for dynamically using a WSDL description of a SOAP service for client access.


Home for the Python language: http://www.python.org/

The "Cheese Shop" repository of open source Python packages currently has over 1300 entries: http://www.python.org/pypi

A compact survey of Python with links to many resources: https://en.wikipedia.org/wiki/Python_language

The Web Services for Python project at SourceForge: http://pywebsvcs.sourceforge.net/

An implementation of Python in Java - known as Jython: http://www.jython.org/

This was last published in May 2006

Dig Deeper on Managing microservices architectures and SOA

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.