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-8
from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers.clazz import ClassSerializer
from 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 = String
class 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_status
def 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 CdrstatusService
from cdrcheck import make_client
import lxml.etree as et
a = make_client()
b=a.get_Cdrstatus('ss')
for each in b:
print each.sn,each.reporttime,each.hostname