XML technologies / XML / Python & XML / Python, web services and SOAP / SOAP options in Python

(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)

Python’s web-service support is still emerging and incomplete. At present there is lively debate in the XML-SIG (Python XML Special Interest Groups) about SOAP client and server implementations and their interoperability.

SOAP and WSDL, and thus web services, are largely driven by companies such as IBM and Microsoft. Microsoft provides robust client and server support through its SOAP Toolkit, while IBM contributes substantially to Apache SOAP. Both camps support two common ways to access web services: RPC-style proxies and SOAP serialization.

You can already implement Python web-service clients easily using Python bridges to subsystems such as COM and Java. Python’s COM support is excellent and becomes available by installing PythonCOM, part of the win32all.exe package from ActiveState’s website. Detailed instructions appear under “Requirements for using MSSOAP.”

With today’s web-service APIs, you can probably modify your Python code quickly to use native Python support once it matures. It might, however, be preferable to write the logic in Python while calling APIs implemented in faster C++. Python is an excellent “glue language” because of its robust object model, sophisticated text and file handling, and component access. Resource-intensive components, such as parsers or SOAP clients, may work better written in fast native code and controlled by Python.

With one cross-platform exception, this section’s Python clients mainly use COM to bridge to Microsoft’s mature SOAP serializer and connector. They therefore run mainly on Windows. The section also covers SOAPy, a native Python implementation of an RPC-style SOAP client. SOAPy should run wherever Python runs.

Working with SOAPy

SOAPy currently supports RPC-style interactions with web services published through WSDL. SOAPy is currently available on SourceForge. Being a native Python implementation, SOAPy is inherently cross-platform.

SOAPy is very easy to use: it transparently represents a remote web service as though it were a local Python object. Its source distributions include examples demonstrating its capabilities. In the included get_temperature example, you can enter a ZIP code and ask a remote weather service for the current temperature. Here is how to request the temperature in Woodinville, Washington:

C:\c9>python get_temperature.py 98072
Temperature for 98072 = 53.0 degrees F

Impressive as this application is, it immediately makes one curious about how SOAPy works. SOAPy performs this trick in three lines of code:

import soap
server = soap.get_proxy('http://www.xmethods.net/sd/TemperatureService.wsdl')

temperature = server.getTemp(zipcode=zip)

The secret is that SOAPy interprets the WSDL file and creates a local proxy object apparently possessing every method of the remote service.

Ideally, when using SOAPy, get_proxy is the only method you call. You should then be able to use the methods described in the WSDL file hosted by the remote service.

Working with MSSOAP

MSSOAP lets you choose between an RPC client and a serialization client.

The RPC client works much like SOAPy, with subtle differences. For instance, to initialize a connection to a remote service description, use the method mssoapinit instead of SOAPy’s get_proxy, but the ultimate effect is the same, as shown here:

import win32com.client

sc = win32com.client.Dispatch("MSSOAP.SoapClient")
sc.mssoapinit("http://WebServiceDomain/service.wsdl")

response = sc.methodName(param, param)

print(response)

Again, RPC web-service implementations let you work with remote objects as though they were local. Serialization works somewhat differently, giving finer control over the structure of SOAP requests and letting you use a service without necessarily relying on a WSDL description file.

MSSOAP serialization basics

Serialization requires more work than RPC, with its own advantages and disadvantages. It gives precise control over the appearance of a SOAP XML request. It also lets you interact with a web service without understanding WSDL, which may be valuable as client and server SOAP implementations mature.

The main difference between RPC and serialization is the WSDL file. It supplies the RPC implementation with the service endpoint information it needs, including the URIs and namespaces involved, and parameters and their types. Without RPC and WSDL you must supply these additional details manually.

Adding URIs and namespaces

Creating a SOAP packet with MSSOAP requires several objects, starting with the connector and the serializer. Supply the connector with information normally found in WSDL: for example, the endpoint, SOAP Action URI and namespace:

import win32com.client

SoapActionUri = "http://tempuri.org/action/Calc.Add"
ElementNamespace = "http://tempuri.org/message/"
EndPointUrl = \"http://centauri/MSSoapSamples/Calc/Service/SrSz/AspVbs/Calc.asp"

connector = win32com.client.Dispatch("MSSOAP.HttpConnector")
connector.SetProperty("EndPointURL", EndPointUrl)
connector.SetProperty("SoapAction", SoapActionUri)
connector.BeginMessage( )

The connector is now ready to connect to the service. You need only prepare the SOAP envelope and make the call. The envelope is also prepared manually.

serializer = win32com.client.Dispatch("MSSOAP.SoapSerializer")
serializer.Init(connector.InputStream)

Once the serializer has been created, attach it to the connector for writing to the service; it is thus attached.

Creating the SOAP envelope

Use the methods of serializer to build the SOAP packet and specify its parameters, including the target method. The following lines prepare a packet for a calculator service expecting integer parameters A and B for a method named Add:

# Erzeuge SOAP-Umschlag
serializer.startEnvelope( )
serializer.startBody( )
serializer.startElement("Add", ElementNamespace, '', "m")
serializer.startElement("A")
serializer.writeString("4")
serializer.endElement( )
serializer.startElement("B")
serializer.writeString("5")
serializer.endElement( )
serializer.endElement( )
serializer.endBody( )
serializer.endEnvelope( )

# Beende SOAP-Nachricht
connector.EndMessage( )

As the code above shows, the connector is then told by calling EndMessage that the complete SOAP message has been prepared.

Making the call

After constructing the serializer and connector, the final step is to instantiate a reader object to check the service for errors and retrieve the call’s result.

reader = win32com.client.Dispatch("MSSOAP.SoapReader")
reader.Load(connector.OutputStream)

Here the reader is associated with the output stream of the connector to obtain the service call’s result. The Fault attribute of the reader indicates success or failure.

if reader.Fault:
   print("Fehler: ", reader.faultstring.Text)

print reader.RPCResult.Text

The service response is in the reader.RPCResult object. Here the calculator returns “9”, the sum of the supplied parameters 4 and 5.

  

 

 

 

back to top