You are here: Home > Dive Into Python > SOAP Web Services > Introspecting SOAP Web Services with WSDL | << >> | ||||
Dive Into PythonPython from novice to pro |
Like many things in the web services arena, WSDL has a long and checkered history, full of political strife and intrigue. I will skip over this history entirely, since it bores me to tears. There were other standards that tried to do similar things, but WSDL won, so let's learn how to use it.
The most fundamental thing that WSDL allows you to do is discover the available methods offered by a SOAP server.
Example 12.8. Discovering The Available Methods
>>> from SOAPpy import WSDL >>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl') >>> server = WSDL.Proxy(wsdlFile) >>> server.methods.keys() [u'getTemp']
Okay, so you know that this SOAP server offers a single method: getTemp. But how do you call it? The WSDL proxy object can tell you that too.
Example 12.9. Discovering A Method's Arguments
>>> callInfo = server.methods['getTemp'] >>> callInfo.inparams [<SOAPpy.wstools.WSDLTools.ParameterInfo instance at 0x00CF3AD0>] >>> callInfo.inparams[0].name u'zipcode' >>> callInfo.inparams[0].type (u'http://www.w3.org/2001/XMLSchema', u'string')
WSDL also lets you introspect into a function's return values.
Example 12.10. Discovering A Method's Return Values
>>> callInfo.outparams [<SOAPpy.wstools.WSDLTools.ParameterInfo instance at 0x00CF3AF8>] >>> callInfo.outparams[0].name u'return' >>> callInfo.outparams[0].type (u'http://www.w3.org/2001/XMLSchema', u'float')
Let's put it all together, and call a SOAP web service through a WSDL proxy.
Example 12.11. 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) >>> server.getTemp('90210') 66.0 >>> server.soapproxy.config.dumpSOAPOut = 1 >>> server.soapproxy.config.dumpSOAPIn = 1 >>> temperature = server.getTemp('90210') *** Outgoing SOAP ****************************************************** <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:getTemp xmlns:ns1="urn:xmethods-Temperature" SOAP-ENC:root="1"> <v1 xsi:type="xsd:string">90210</v1> </ns1:getTemp> </SOAP-ENV:Body> </SOAP-ENV:Envelope> ************************************************************************ *** Incoming SOAP ****************************************************** <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <ns1:getTempResponse xmlns:ns1="urn:xmethods-Temperature" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <return xsi:type="xsd:float">66.0</return> </ns1:getTempResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> ************************************************************************ >>> temperature 66.0
<< Introducing WSDL |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
Searching Google >> |