| 導航:起始頁 > Dive Into Python > SOAP Web 服務 > SOAP 網絡服務查錯 | << >> | ||||
深入 Python :Dive Into Python 中文版Python 從新手到專家 [Dip_5.4b_CPyUG_Release] |
|||||
SOAP 提供了一個很方便的方法用以查看背后的情形。
SOAPProxy 的兩個小設置就可以打開查錯模式。
>>> from SOAPpy import SOAPProxy >>> url = 'http://services.xmethods.net:80/soap/servlet/rpcrouter' >>> n = 'urn:xmethods-Temperature' >>> server = SOAPProxy(url, namespace=n)>>> server.config.dumpSOAPOut = 1
>>> server.config.dumpSOAPIn = 1 >>> temperature = server.getTemp('27502')
*** 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">27502</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">80.0</return> </ns1:getTempResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope> ************************************************************************ >>> temperature 80.0
大部分 XML 請求文檔都基于模板文件。忽略所有命名空間聲明這些對于所有 SOAP 調用都一成不變的東西。這個 “函數調用” 的核心是<Body> 當中的部分:
<ns1:getTempxmlns:ns1="urn:xmethods-Temperature"
SOAP-ENC:root="1"> <v1 xsi:type="xsd:string">27502</v1>
</ns1:getTemp>
| 這個元素名 getTemp 就是函數名。SOAPProxy 使用 getattr 作為分發器。有別于使用方法名分別調用本地方法,這里使用方法名構造了一個 XML 請求文檔。 | |
| 函數的 XML 元素被存儲于一個特別的命名空間,這個命名空間就是你在建立 SOAPProxy 對象時所指定的那個命名空間。也不必為 SOAP-ENC:root 而苦惱,因為它也是基于模板文件的。 | |
| 函數的參數也被記入 XML 文檔。SOAPProxy 查看并確定每個參數的數據類型 (這里是 string 字符串類型)。參數的數據類型記入 xsi:type 屬性,并在其后記入實際的字符串值。 |
返回的 XML 文檔同樣容易理解,重點在于知道應該忽略掉哪些內容。把注意力集中在 <Body> 部分:
<< 步入 SOAP |
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
WSDL 介紹 >> |