加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

soaplib实现Webservice

发布时间:2020-12-17 00:23:51 所属栏目:安全 来源:网络整理
导读: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,解

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读