如何用python实现soap协议并搭建webservice
0?2
网上搜索了一番,用python实现soap的库有好几个,soappy,soaplib,suds等。但是SOAPpy这个库已经好几年没人去维护更新了,所以不予考虑,而suds这个库用来构建soap服务端比较麻烦,所以最终锁定soaplib来实现服务端发布,而用suds实现客户端获取;
0?2
第一步:安装soaplib
0?2
http://pypi.python.org/pypi/soaplib/0.8.1
0?2
0?2 ?0?2 * 想要运行soaplib还必须依赖 libxml2及libxslt这二个包;google之后总结以下安装过程
0?2
第二步:安装libxml2
0?2?0?2 ?0?2
0?2 ?0?2 * 当一切都没有错的时候,你会在/usr/local里看到生成一个libxml2的文件夹
0?2
第三步:安装libxslt
0?2?0?2 ?0?2
0?2 ?0?2 * 当一切都没有错的时候,你会在/usr/local里看到生成一个libxslt的文件夹
0?2
第四步:复制文件
0?2
0?2 ?0?2 至此,你可以使用soaplib开始来发布你的webservice了
0?2
0?2 ?0?2 . 下面是一个简单的'hello world'的server.py及client.py
#server.pyfrom soaplib.service import soapmethodfrom soaplib.serializers.primitive import String, Integer, Arrayfrom soaplib.wsgi_soap import SimpleWSGISoapApp class HelloWorldService(SimpleWSGISoapApp): @soapmethod(String, _returns=String) def says(self,name): return nameif __name__=='__main__': try: from wsgiref.simple_server import make_server server = make_server('192.168.0.219', 7889, HelloWorldService()) print "listening on 0.0.0.0:7889" server.serve_forever() except ImportError: print "Error: example server code requires Python >= 2.5"
0?2
0?2
0?2 ?0?2 . soaplib实现client
#client.pyfrom server import HelloWorldServicedef make_client(): from soaplib.client import make_service_client client = make_service_client('http://192.168.0.219:7889', HelloWorldService()) return client a = make_client() print a.says('hello,world')
0?2
0?2 ?0?2
0?2 ?0?2. 下面看看suds如何实现client
0?2 https://fedorahosted.org/releases/s/u/suds/python-suds-0.4.tar.gz
0?2
#client.pyfrom suds.client import Clienturl = "http://192.168.0.219:7889?wsdl"client = Client(url)print client.service.says('hello world')
0?2
0?2
0?2 . 通过比较可以看出,用suds更简洁更方便,
0?2 .https://fedorahosted.org/suds/wiki/Documentation
0?2
联系客服