当前位置: 移动技术网 > IT编程>脚本编程>Python > 利用soaplib搭建webservice详细步骤和实例代码

利用soaplib搭建webservice详细步骤和实例代码

2019年04月02日  | 移动技术网IT编程  | 我要评论

莽女追魂,lifetime电视台,终极斗士4高清

最近在搞基于python的webservice项目,今天为把环境给配好,折腾了不少时间,还是把配的过程记录下来,以后备用:
首先你系统上要有python,这个不必说啦,我系统上用的是2.7+
其次,要用python进行webservice开发,还需要一些库:
lxml :
命令行下 sudo easy_install lxml 就能安装

pytz :
 命令行下 sudo easy_install pytz 就能安装

soaplib:
进行webservice开发必须要用的库,可以在https://github.com/volador/soaplib拿到,注意要先安装上面两个插件再安装这个,因为这个依赖于上面两个插件,把zip拿下来后解压,sudo python setup.py install 就能安装了。

复制代码 代码如下:

soaplib is an easy to use python library for publishing soap web services using wsdl 1.1 standard, and answering soap 1.1 requests. with a very small amount of code, soaplib allows you to write a useful web service and deploy it as a wsgi application.

soaplib是python的soap框架,可以用来建立webservice.soaplib这样在这里下载:https://github.com/volador/soaplib

装好soaplib后新建一个test.py,建立一个webservice,实现返回两个整数相加的和。代码如下:

复制代码 代码如下:

# -*- coding: cp936 -*-
import soaplib
from soaplib.core.util.wsgi_wrapper import run_twisted #发布服务
from soaplib.core.server import wsgi
from soaplib.core.service import definitionbase  #所有服务类必须继承该类
from soaplib.core.service import soap  #声明注解
from soaplib.core.model.clazz import array #声明要使用的类型
from soaplib.core.model.clazz import classmodel  #若服务返回类,该返回类必须是该类的子类
from soaplib.core.model.primitive import integer,string
class c_probecdrmodel(classmodel):
        __namespace__ = "c_probecdrmodel"
        name=string
        id=integer
class additionservice(definitionbase):  #this is a web service     
        @soap(integer,integer,_returns=string)
        def addition(self,a,b):
                return str(a)+'+'+str(b)+'='+str(a+b)      
        @soap(_returns=array(string))
        def getcdrarray(self):
                l_result=["1","2","3"]
                return l_result
        @soap(_returns=c_probecdrmodel)
        def getcdr(self): #返回的是一个类,该类必须是classmodel的子类,该类已经在上面定义
                l_model=c_probecdrmodel()
                l_model.name=l_model.name
                l_model.id=l_model.id
                return l_model   

       
if __name__=='__main__':  #发布服务      
        try:
                print '服务已经开启'
                from wsgiref.simple_server import make_server
                soap_application = soaplib.core.application([additionservice], 'tns')
                wsgi_application = wsgi.application(soap_application)
                server = make_server('localhost', 7789, wsgi_application)
                server.serve_forever()

        except importerror:
                print 'error'

在浏览器中访问http://127.0.0.1:7789/soap/?wsdl出现一大版的xml而不是访问错误,就说明服务添加成功啦
在命令行下既可以测试

复制代码 代码如下:

>>>from suds.client import client
>>> test=client('http://localhost:7789/soap/?wsdl')
>>> print test.service.addition(1,2)
1+2=3

问题注意:代码运行过程中,会出现各种模块没找到,那是因为你没有安装,根据提示,google搜索下载安装就好,如果是windows,找不到exe,zip格式的安装文件,下载tar.gz也行的,解压后在cmd切换到解压目录,执行:python setup.py install便安装成功了。
第一次建立连接很慢很慢,慢到无法忍受,>>> test=client('http://localhost:7789/soap/?wsdl'),不知道是怎么回事。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网