| 導航:起始頁 > Dive Into Python > SOAP Web 服務 > 以 WSDL 進行 SOAP 內省 | << >> | ||||
深入 Python :Dive Into Python 中文版Python 從新手到專家 [Dip_5.4b_CPyUG_Release] |
|||||
就像網絡服務舞臺上的所有事物,WSDL 也經歷了一個充滿明爭暗斗而且漫長多變的歷史。我不打算講述這段令我傷心的歷史。還有一些其他的標準提供相同的支持,但 WSDL 還是勝出,所以我們還是來學習一下如何使用它。
WSDL 最基本的功能便是讓你揭示 SOAP 服務器所提供的有效方法。
>>> from SOAPpy import WSDL>>> wsdlFile = 'http://www.xmethods.net/sd/2001/TemperatureService.wsdl' >>> server = WSDL.Proxy(wsdlFile)
>>> server.methods.keys()
[u'getTemp']
好的,你知道這個 SOAP 服務器提供一個方法:getTemp。但是如何去調用它呢?WSDL 也在這方面提供信息。
>>> 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 還允許你自省函數的返回值。
>>> 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')
讓我們整合一下,通過 WSDL proxy 調用一個 SOAP 網絡服務。
>>> 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
<< WSDL 介紹 |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
搜索 Google >> |