用python的soaplib模块实现WSDL和SOAP协议的web service

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值