用python的soaplib模块实现WSDL和SOAP协议的web service
来源:互联网 发布:淘宝聚星台是什么 编辑:程序博客网 时间:2024/06/06 07:33
IBM开发者网络上和dive into python上的python webservice的例子大多是用ZSI和SOAPpy实现的基于WSDL和SOAP协议的webservice,但这两个包已经三四年没有维护了,它们依赖的包也不再维护了,在python2.5以上版本中安装时也会有警告。
好在有一个更好的包可以实现WSDL和SOAP,它就是soaplib,目前版本是0.8.1,可以通过eazy_install进行安装(soaplib依赖lxml和pytz)。
官方URL:http://wiki.github.com/jkp/soaplib/
但这个包的文档几乎没有,文档URL:http://github.com/jkp/soaplib/blob/master/README.markdown
soaplib实现WSDL和SOAP协议的机制:
先写python程序,发布成web service之后,可以通过url获取到soaplib自动生成的wsdl文件。
以下是server的示例代码:
# coding: utf-8from soaplib.wsgi_soap import SimpleWSGISoapAppfrom soaplib.service import soapmethodfrom soaplib.serializers.clazz import ClassSerializerfrom soaplib.serializers.primitive import String, Integer, Array, DateTime'''This is a simple cdrcheck program writing using soaplib, starting a server, and creating a service client. '''class Cdrstatus(ClassSerializer): """ the class the soapmethod will return. """ class types: hostname = String ip = String cdrtype = String status = Integer comment = String reporttime = String sn = Stringclass CdrstatusService(SimpleWSGISoapApp): @soapmethod(String, _returns=Array(Cdrstatus)) def get_Cdrstatus(self,user): ''' Docstrings for service methods appear as documentation in the wsdl <b>return the status of each kind of cdrs</b> @return the Cdrstatus. 这个方法接受一个参数,客户端在调用这个方法时,必须传一个参数 返回值是一个列表,列表中的元素是Cdrstatus类的实例 ''' from cdrstat import cdr_stat cdr_status_dict = cdr_stat() cdr_status = [] for probe_info in cdr_status_dict.keys(): for cdrtype in cdr_status_dict[probe_info].keys(): #实例化Cdrstatus类 c = Cdrstatus() c.hostname = probe_info[1] c.ip = probe_info[2] c.cdrtype = cdrtype c.status = cdr_status_dict[probe_info][cdrtype][1] c.comment = cdr_status_dict[probe_info][cdrtype][2] c.reporttime = cdr_status_dict[probe_info][cdrtype][0] c.sn = cdr_status_dict[probe_info][cdrtype][3] cdr_status.append(c) return cdr_statusdef make_client(): """ 这个函数是这个web service的客户端,在服务端程序中不是必须的。 """ from soaplib.client import make_service_client #client = make_service_client('http://10.168.68.18:8000/',CdrstatusService()) client = make_service_client('http://10.168.86.169:7889/',CdrstatusService()) return client if __name__=='__main__': try: from wsgiref.simple_server import make_server #10.168.86.169,7889为服务绑定的IP地址和端口 #这个WS的WSDL的URL为http://10.168.86.169:7889/cdrcheck?wsdl server = make_server('10.168.86.169', 7889, CdrstatusService()) server.serve_forever() except ImportError: #wsgiref这个模块需要python2.5以上的版本,可以使用其它服务器进行部署 print "Error: example server code requires Python >= 2.5"
运行程序后,通过浏览器访问这个WS的WSDL的URL:http://10.168.86.169:7889/cdrcheck?wsdl 如果访问时报错,请删除程序中的中文注释。
客户端代码如下:
cdrcheck_client.py
from cdrcheck import CdrstatusServicefrom cdrcheck import make_clientimport lxml.etree as eta = make_client()b=a.get_Cdrstatus('ss')for each in b: print each.sn,each.reporttime,each.hostname
- 用python的soaplib模块实现WSDL和SOAP协议的web service
- 用python的soaplib模块实现WSDL和SOAP协议的web service
- Web Service, SOAP和WSDL的关系
- Web Service, SOAP和WSDL的关系
- Web Service、WSDL、SOAP、HTTP的关系
- SOAP/Web Service/WSDL的关系
- SOAP web service with soaplib 0.9+
- SOAP web service with soaplib 2.0
- CakePHP下利用soap协议实现web service的例子
- WSDL & SOAP & Web Service
- Web-Service-SOAP&WSDL
- 白痴理解的SOAP/Web Service/WSDL关系
- 白痴理解的SOAP/Web Service/WSDL关系
- 白痴理解的SOAP/Web Service/WSDL关系
- 白痴理解的SOAP/Web Service/WSDL关系
- 白痴理解的SOAP/Web Service/WSDL关系
- 白痴理解的SOAP/Web Service/WSDL关系
- Web Service概述 --SOAP协议与 WSDL简介
- tar的常用方法(压缩/解压/gzip/bzip2)
- 文件传输协议(FTP)必将消亡
- 程序员每月2000刀,多了还是少了?
- 当一个窗口关闭时WM_CLOSE,WM_DESTROY,WM_QUIT
- Extjs嵌套数据类型定义和显示 (一)
- 用python的soaplib模块实现WSDL和SOAP协议的web service
- 当php配置文件中魔术引号方法没有开启时的解决方案
- 计算机端口及设置
- flush让页面分块,逐步呈现
- Android ApiDemo学习(四)Views——7 Layout Animation
- LAST_QUERY in Form and OAF Page
- Linux php的运行模式与其相关名词术语
- Algorithms,part1 week1
- lucene管理IndexReader和IndexWriter的最佳实践