现在的位置: 首页 > 综合 > 正文

soaplib实现Webservice

2018年02月20日 ⁄ 综合 ⁄ 共 1783字 ⁄ 字号 评论关闭

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, Dave",
      "Hello, Dave",
      "Hello, Dave",
      "Hello, Dave",

 参考消息:

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

抱歉!评论已关闭.