cmdbuild的部署可以查看文章:http://20988902.blog.51cto.com/805922/1541289
部署成功后,訪問http://192.168.1.1:8080/cmdbuild/services/soap/ 就能看到所有的webservice方法,證明server這邊已經ready了
cmdbuild webservice官方說明文檔:http://download.csdn.net/detail/siding159/7888309
下面是使用python開發webservice client的方法:
1.模塊
python的webservice模塊有很多,這里使用suds來進行連接
可以通過 easy_install suds來安裝suds,十分方便
suds官方文檔:https://fedorahosted.org/suds/wiki/Documentation
2.日志
suds是通過logging來實現日志的,所以配置logging后,就可以看到soap連接過程中的所有數據
import logging logging.basicConfig(level=logging.DEBUG, filename='myapp.log') logging.getLogger('suds.client').setLevel(logging.DEBUG) # logging.getLogger('suds.transport').setLevel(logging.DEBUG) # logging.getLogger('suds.xsd.schema').setLevel(logging.DEBUG) # logging.getLogger('suds.wsdl').setLevel(logging.DEBUG)
suds提供了四種日志類型,可以根據需要來配置
3.連接
from suds.client import Client url = 'http://192.168.8.190:8080/cmdbuild/services/soap/Private?wsdl' client = Client(url) print client r=client.service.getCard('Cabinet',2336)
實例化Client后,可以print client來輸出次webservice提供的接口,也就是wsdl文件的說明
通過r=client.service.getCard('Cabinet',2336)來調用webservice的getCard接口,並輸入參數,print client的輸出結果會有每個接口需要輸入的參數說明
但是這樣連接后,會報錯:suds.WebFault: Server raised fault: 'An error was discovered processing the <wsse:Security> header'
因為cmdbuild需要wsse安全驗證
4.wsse安全驗證
為client實例設置wsse參數
from suds.wsse import *
from suds.client import Client
url = 'http://192.168.8.190:8080/cmdbuild/services/soap/Private?wsdl' client = Client(url) security = Security() token = UsernameToken('admin', 'admin') security.tokens.append(token) client.set_options(wsse=security) r=client.service.getCard('Cabinet',2336)
UsernameToken的參數是訪問的賬號和密碼
繼續運行程序后,可能會報錯:suds.WebFault: Server raised fault: 'The security token could not be authenticated or authorized'‘
解決方法是,把cmdb的auth配置文件的force.ws.password.digest參數設置為false,並重啟tomcat,auth.conf路徑 tomcat/webapps/WEB-INI/conf/auth.conf
5.處理返回來的xml
繼續運行,會報錯:xml.sax._exceptions.SAXParseException: <unknown>:2:0: syntax error
原因是程序在解析返回的xml文件的時候出錯了,通過日志,我們可以看到返回來的body是這樣的
--uuid:b16cf808-f566-4bad-a92c-a7d303d33211 Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"; Content-Transfer-Encoding: binary Content-ID: <root.message@cxf.apache.org> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:getCardResponse xmlns:ns2="http://soap.services.cmdbuild.org"><ns2:return><ns2:attributeList><ns2:name>User</ns2:name><ns2:value>admin</ns2:value></ns2:attributeList><ns2:attributeList><ns2:code>6084</ns2:code><ns2:name>CabinetStatus</ns2:name><ns2:value>終止</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>RentalAmount</ns2:name><ns2:value>9000.00</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>Description</ns2:name><ns2:value>機房 Room 1 D14</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>BeginDate</ns2:name><ns2:value>12/08/14 18:03:17</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>Contract</ns2:name><ns2:value></ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>Code</ns2:name><ns2:value>D14</ns2:value></ns2:attributeList><ns2:attributeList><ns2:code>168</ns2:code><ns2:name>Trusteeship</ns2:name><ns2:value>科技有限公司</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>Notes</ns2:name><ns2:value></ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>Status</ns2:name><ns2:value>N</ns2:value></ns2:attributeList><ns2:attributeList><ns2:code>2325</ns2:code><ns2:name>Room</ns2:name><ns2:value>機房Room 1</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>StartDate</ns2:name><ns2:value>23/09/10</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>Id</ns2:name><ns2:value>2336</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>Capacity</ns2:name><ns2:value>0</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>NoUsed</ns2:name><ns2:value>0</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>IdClass</ns2:name><ns2:value>42216</ns2:value></ns2:attributeList><ns2:attributeList><ns2:name>CibinetNUm</ns2:name><ns2:value>15</ns2:value></ns2:attributeList><ns2:beginDate>2014-08-12T18:03:17.839+08:00</ns2:beginDate><ns2:className>Cabinet</ns2:className><ns2:id>2336</ns2:id><ns2:metadata><ns2:key>runtime.privileges</ns2:key><ns2:value>write</ns2:value></ns2:metadata><ns2:user>admin</ns2:user></ns2:return></ns2:getCardResponse></soap:Body></soap:Envelope> --uuid:b16cf808-f566-4bad-a92c-a7d303d33211--
發現在xml字符串之外還有很多沒用的信息,所以我們需要在接受了返回之后,對返回結果進行處理(把它轉化成標准的xml字符串)后,再進行xml的解析
import re from suds.wsse import * from suds.client import Client from suds.plugin import MessagePlugin url = 'http://192.168.8.190:8080/cmdbuild/services/soap/Private?wsdl' class MyPlugin(MessagePlugin): def received(self, context): reply_new=re.findall("<soap:Envelope.+</soap:Envelope>",context.reply,re.DOTALL)[0] context.reply=reply_new client = Client(url, plugins=[MyPlugin()]) security = Security() token = UsernameToken('admin', 'admin') security.tokens.append(token) client.set_options(wsse=security) r=client.service.getCard('Cabinet',2336) print r
做法就是定義一個鈎子(hook),在接收返回后,把需要的xml字符串抽取處理,由於xml中可能有換行符,所以加入了re.DOTALL
6.返回值
這樣程序就能正常訪問webservice了,返回值:
(card){ attributeList[] = (attribute){ name = "User" value = "admin" }, (attribute){ code = "6084" name = "CabinetStatus" value = "合同" }, (attribute){ name = "RentalAmount" value = "9000.00" }, (attribute){ name = "Description" value = "機房Room 1 D14" }, (attribute){ name = "BeginDate" value = "12/08/14 18:03:17" }, (attribute){ name = "Contract" value = None }, (attribute){ name = "Code" value = "D14" }, (attribute){ code = "168" name = "Trusteeship" value = "科技有限公司" }, (attribute){ name = "Notes" value = None }, (attribute){ name = "Status" value = "N" }, (attribute){ code = "2325" name = "Room" value = "機房Room 1" }, (attribute){ name = "StartDate" value = "23/09/10" }, (attribute){ name = "Id" value = "2336" }, (attribute){ name = "Capacity" value = "0" }, (attribute){ name = "NoUsed" value = "0" }, (attribute){ name = "IdClass" value = "42216" }, (attribute){ name = "CibinetNUm" value = "15" }, beginDate = 2014-08-12 18:03:17.000839 className = "Cabinet" id = 2336 metadata[] = (metadata){ key = "runtime.privileges" value = "write" }, user = "admin" }
一看我就暈了,這是什么數據結構?type返回的類型是instance
不過我發現可以用列表的方式來訪問它,例如輸入r[0][1][2] 返回的是“合同”,不知道還有沒有更好的返回結果解析方式
后記:
url的說明:soap的url必須要加端口,例如8080,不然會返回 urllib2.URLError: <urlopen error [Errno 111] Connection refused>
class MyPlugin(MessagePlugin): def marshalled(self, context): pass_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText' password = context.envelope.getChild('Header').getChild('Security').getChild('UsernameToken').getChild( 'Password') password.set('Type', pass_type)
轉載請帶上我(http://i.cnblogs.com/EditPosts.aspx?postid=3963831)