微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

soaplib实现Webservice

1、安装setuptools,执行如下命令

 sudo apt-get install python-setuptools

安装该软件是为了下面使用easy_install命令安装soaplib

2、安装soaplib

soaplib依赖于Twisted、lxml,而lxml依赖于libxml和libxslt

2.1安装libxml

下载libxml2-2.7.2.tar.gz,解压cd到解压目录,执行如下命令:

./configure

make

make install

2.2安装libxslt,在线安装,执行如下命令:

sudo apt-get install libxslt-dev

2.3 安装pytz,下载pytz-2012h.tar.bz,解压,进入解压目录,执行如下命令:

sudo python setup install

2.4 安装soaplib,执行如下命令:

easy_install soaplib

2.5 客户端需要安装suds,从https://pypi.python.org/pypi/suds下载suds.tar.gz,解压,进入解压路径,执行如下命令

sudo python setup install

3、编写服务端代码,如下:

import soaplib
from soaplib.core.service import rpc,DeFinitionBase
from soaplib.core.model.primitive import String,Integer
from soaplib.core.server import wsgi
from soaplib.core.model.clazz import Array
from soaplib.core.service import soap

class HelloWorldService(DeFinitionBase):
    @soap(String,Integer,_returns=Array(String))
    def say_hello(self,name,times):
        results = []
        for i in range(0,times):
            results.append('Hello,%s'%name)
        return results

if __name__=='__main__':
    try:
        from wsgiref.simple_server import make_server
        soap_application = soaplib.core.Application([HelloWorldService],'tns')
        wsgi_application = wsgi.Application(soap_application)
        server = make_server('localhost',7789,wsgi_application)
        server.serve_forever()
    except ImportError:
        print "Error: example server code requires Python >= 2.5"

启动以上程序, 在浏览器重访问 http://127.0.0.1:7789/SOAP/?wsdl,如 果正常的话,则能看到该服务的描述信息,包括各个方法的输入参数、返回值,以及实体类的信息。

4、编写客户端程序:

from suds.client import Client
hello_client = Client('http://localhost:7789/?wsdl')
result = hello_client.service.say_hello("Dave",5)
print result

运行结果如下

string[] = 
      "Hello,Dave",
      "Hello,


 参考消息:

http://blog.csdn.net/kerwin_liu/article/details/5777737

https://pypi.python.org/pypi/suds

http://www.cnblogs.com/grok/archive/2012/04/29/2476177.html

http://soaplib.github.com/soaplib/2_0/pages/helloworld.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐