Python SOAP Clients
Motivation
You want to invoke web services methods using python code.
There are few python libraries for python web service.
2 commonly used and stable python libraries to invoking web services are:
SOAPpy and ZSI
see http://pywebsvcs.sourceforge.net/
SOAPpy
Use SOAPpy for simple soap method calls that expect only simple type arguments(string, int, etc)
Setup
Follow the steps in http://www.diveintopython.org/soap_web_services/index.html
Sample Code
- List available methods offered by a SOAP server
from SOAPpy import WSDL
wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl')
server = WSDL.Proxy(wsdlFile)
print server.methods.keys()
- Calling a web service through a WSDL Proxy
from SOAPpy import WSDL
wsdlFile = ‘http://www.xmethods.net/sd/2001/TemperatureService.wsdl’)
server = WSDL.Proxy(wsdlFile)
print server.getTemp(‘90210′)
server.soapproxy.config.dumpSOAPOut = 1
server.soapproxy.config.dumpSOAPIn = 1
temperature = server.getTemp(‘90210′)
print temperature
See http://www.diveintopython.org/soap_web_services/introspection.html
ZSI (Zolera Soap Infrastructure)
Use ZSI for simple/complex WSDL types.
Setup
1. Download the latest ZSI Egg file from here
1. Download the Easy Install tool from here.
1. Follow this to install the ZSI python egg file
Code Generation from WSDL and XML Schema
In WSDL complex types, objects might be required to be the argument to the SOAP method you want to invoke. For example, the soap method might required a Shipment object as its argument, so how do you pass in an object which your client currently do not possess?
You can create the class(s) required through file generation by passing in the WSDL url.
For python, we will be using the wsdl2py script which comes with ZSI.
1. Go to your PYTHON_HOME\Scripts , there shld be a wsdl2py file there.
1. From the same directory, run this command
wsdl2py -bu http://webservices.wolfram.com/services/SearchServices/WolframSearch2.wsdl
1. The classes that you require are generated in the same directory and available to be used.
Sample Code
- Usage of the generated code to invoke web service
Using the previous example on WolframSearch,
# import the generated class stubs
from WolframSearchService_services import *
# get a port proxy instance
loc = WolframSearchServiceLocator()
port = loc.getWolframSearchmyPortType()
# create a new request
req = WolframSearchRequest()
req.Options = req.new_Options()
req.Options.Query = 'newton'
# call the remote method
resp = port.WolframSearch(req)
# print results
print 'Search Time:', resp.Result.SearchTime
print 'Total Matches:', resp.Result.TotalMatches
for hit in resp.Result.Matches.Item:
print '--', hit.Title
Note that WolframSearchRequest is the object which is required by the WolframSearch soap method.
see here for detailed step-by-step explaination
Summary
We covered the 2 most common python web service libraries that can be used as the client to SOAP servers of different programming largely dominated by Java.
I would love to try this but according to the SOAPpy readme (and diveintomark) it requires pyXML, which is no longer maintained. Apparently it doesn’t support Python versions newer than 2.4.
Charlie
August 17, 2007 at 11:47 am
Thanks for your feedback charlie.
I’m using python 2.3 and 2.4, thus probably didn’t affect me.
How about using ZSI?
Best Wishes,
Kenny Lee
kenny lee
August 17, 2007 at 4:52 pm
One observation and a couple of complaints.
If you are running Ubuntu SOAPpy and its dependencies are available in the Universe repository as a deb package. Easy and quick. The pacage was assembled by the MOTU team so it has support behind it.
The complaints are minor but impacting. The first one is the DIPy docs in Chapter 12. Many of the examples are outdated though the code is correct. The same for many other online docs. Many authors reference SOAP services from XMission. The problem again is that the many examples refer to services no longer available.
When you try to get your head WSDL, XML, services and elements and how they relate to the python calls. Its a steep learning curve without it.
JohnMc
January 17, 2008 at 3:33 am
I can tell that this is not the first time at all that you mention this topic. Why have you chosen it again?
How to Get Six Pack Fast
April 15, 2009 at 5:00 pm