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 = ""
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 = ""
name = "Trusteeship"
value = "科技有限公司"
},
(attribute){
name = "Notes"
value = None
},
(attribute){
name = "Status"
value = "N"
},
(attribute){
code = ""
name = "Room"
value = "机房Room 1"
},
(attribute){
name = "StartDate"
value = "23/09/10"
},
(attribute){
name = "Id"
value = ""
},
(attribute){
name = "Capacity"
value = ""
},
(attribute){
name = "NoUsed"
value = ""
},
(attribute){
name = "IdClass"
value = ""
},
(attribute){
name = "CibinetNUm"
value = ""
},
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>

CMDBuild更新后,请求接口的时候客户端报错:
xml.sax._exceptions.SAXParseException: <unknown>:1:0: syntax error
查看客户端的日志显示:
A security error was encountered when verifying the message
查看CMDBuild的日志,显示:
Caused by: org.apache.wss4j.common.ext.WSSecurityException: BSP:R4201: Any PASSWORD MUST specify a Type attribute
即任何密码都必须定义Type属性。
发现需要在Password标签中设置属性:
Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'
再查看了suds的文档,发现可以通过设置Hook来修改suds发送到cmdbuild的请求数据:
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)
marshalled方法会在suds发送数据给cmdbuild前执行。
 

转载请带上我(http://i.cnblogs.com/EditPosts.aspx?postid=3963831)

												

python通过webservice连接cmdbuild的更多相关文章

  1. Python调用Webservice

    使用Python调用webservice 推荐使用 suds包 该包一般在Python2.x   python3各种麻烦 略过 实例 import suds # webservice url url ...

  2. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  3. 关于python测试webservice接口的视频分享

    现在大公司非常流行用python做产品的测试框架,还有对于一些快速原型产品的开发也好,很好地支持OO编程,代码易读.Python的更新挺快的,尤其是第三方库. 对于测试人员,代码基础薄弱,用pytho ...

  4. Python中HTTPS连接

    permike 原文 Python中HTTPS连接 今天写代码时碰到一个问题,花了几个小时的时间google, 首先需要安装openssl,更新到最新版本后,在浏览器里看是否可访问,如果是可以的,所以 ...

  5. Python调用Webservice、访问网页

    昨天在调试Webservice的时候,由于不想写测试程序,就想用Python访问Webservice,结果还是相当的麻烦.远没有VSIDE用的方便 不得不说VS还是很强大的,人性化做的很好,不需要你看 ...

  6. python 开发webService

    最近在学习用python 开发webservice,费了半天时间把环境搭好,记录下具体过程,以备后用. 首先系统上要有python.其次要用python进行webservice开发,还需要一些库: 1 ...

  7. 用Python写WebService接口并且调用

    一.用ladon框架封装Python为Webservice接口 另用soaplib实现请看:    http://www.jianshu.com/p/ad3c27d2a946 功能实现的同时,希望将接 ...

  8. 基于Python的Webservice开发(一)-简介

    之前为了解决Webservice的开发,直接用Python自带的CGI模块挂在IIS上. 但是该方式开发Soap的接口,需要大量的开发,而且安全方面也存在很多问题. 我推荐关于用Python开发Web ...

  9. python使用stomp连接activemq

    一.安装ActiveMQ服务 1. 当使用windows时,安装参考:https://blog.csdn.net/WuLex/article/details/78323811 启动:运行activem ...

随机推荐

  1. Basic Example of JMX Technology--转载

    原文地址:http://nick-lab.gs.washington.edu/java/jdk1.5b/guide/jmx/tutorial/connectors.html Basic Example ...

  2. How to Analyze Java Thread Dumps--reference

    原文地址:http://architects.dzone.com/articles/how-analyze-java-thread-dumps The Performance Zone is pres ...

  3. 面试体验:Microsoft 篇(转)

    http://www.cnblogs.com/cathsfz/archive/2012/08/14/microsoft-interview-experience.html 在上一篇<面试体验:G ...

  4. org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto ...

  5. C++之类的析构函数

    一.认识析构函数 在我的前一篇博客介绍了类的构造函数.http://www.cnblogs.com/MrListening/p/5557114.html. 这里我们来简单说说类的析构函数,它是类的一个 ...

  6. fedora 23中配置nfs-server

    fedora 23中配置nfs-server */--> fedora 23中配置nfs-server Table of Contents 1. 产考资料 2. NFS配置文件 2.1. /et ...

  7. linux解压命令笔记

    转载:http://www.cnblogs.com/eoiioe/archive/2008/09/20/1294681.html .tar 解包:tar xvf FileName.tar打包:tar ...

  8. angualrjs学习总结三(http、select、表格、指令以及事件)

    一:http XMLHttpRequest:$http是angularjs的一个核心服务,用于读取远程服务器的数据.$http.get(url) 是用于读取服务器数据的函数.举例:<div ng ...

  9. gvim设置字体和隐藏菜单栏工具栏

    liunx下面设置字体 set guifont=Monaco\ 注意空格的位置,其他写法不认哦! Windows下面设置 set guifont=Monaco:h 隐藏菜单栏 set guioptio ...

  10. WordPress 后台提示输入FTP信息

    Wordpress 安裝主題時,需要輸入ftp訊息 參考了:WordPress后台更新提示输入FTP信息 ,解決方法是: 方法一.通過ssh 對wordpress 項目的文件夾賦權限 sudo cho ...