应用Python开发WebService

2023-01-31 05:01:18 python 开发 Webservice

Why

得益于python领域广泛的功能包,使用Python开发WEBService,实现服务端或客户端,是比较快捷的途径。最近项目上恰巧遇到一个问题,内外系统分别作为客户端与服务端,接口功能已基本确定,但目前双方项目进度不统一。为避免进度受阻,希望能快速开发一个WebService服务端,以验证某些客户端功能是否正常。当然,采用其他工具或方法 也可以快速开发出WebService服务端,但本文仅验证python实现。

How

    准备工作

软件环境:windows + python2.7 + setuptools
工具包:
soaplib
lxml
pytz
twisted
suds
以上安装包,在python安装目录下运行eazy_install (如:eazy_install suds) 即可自动下载安装。

    开发服务端

服务端python源码如下:

# coding: utf-8
import soaplib
import cx_oracle as cx

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, Double
from soaplib.core import Application

# 数据库交互层,模拟一个简单的数据库交互
class DBManage(ClassModel):
    reqNo       = ''
    paramOut    = []

    def __init__(self, reqNo):
        self.reqNo = reqNo

    def exeQuery(self):
        # Connect to database and query values
        conn = cx.connect('db tns connection string') #填写数据库连接字符串
        conn.begin() #开始事务
        print 'connected'
        cursor = conn.cursor()
        cursor.execute("""select 'Hello!' msg from dual""") #查询示例
        rs = cursor.fetchone()
        rtnMsg = rs[0] 
        print rtnMsg # Hello!

        self.paramOut = [rtnMsg]

        conn.commit()  #提交事务
        cursor.close() #关闭资源
        conn.close()   #关闭连接


# 请求信息类
class TestRequestInfo(ClassModel):
    __namespace__ = "TestRequestInfo"
    reqNo           = String


# 返回信息类
class ResultInfo(ClassModel):
    __namespace__ = "ResultInfo"
    reqNo         = String
    resMsg        = String

# 请求方法
def exeRules(reqInfo):
    reqNo = reqInfo.reqNo

    # Query Database and get values
    dm = DBManage(reqNo)
    dm.exeQuery()
    rs = dm.paramOut
    print 'dm invoke ok!'

    resInfo = ResultInfo()
    resInfo.reqNo  = reqNo
    resInfo.resMsg = rs[0]

    #print resInfo
    return resInfo


class TestService(DefinitionBase):  #WebService Method
    @soap(TestRequestInfo,_returns=ResultInfo)
    def getResultInfo(self,reqInfo):
        resInfo = ResultInfo()
        resInfo = exeRules(reqInfo)
        #print resInfo
        return resInfo


if __name__=='__main__':
    try:
        print '服务已启动'
        from wsgiref.simple_server import make_server
        soap_application = Application([TestService], 'tns')
        wsgi_application = wsgi.Application(soap_application)
        server = make_server('localhost', 8899, wsgi_application)
        server.serve_forever()

    except ImportError:
        print 'error'

服务端创建成功并启动,通过浏览器输入 Http://localhost:8899/SOAP?wsdl,可看到接口wsdl,如下图示:
接口wsdl结构

    创建客户端

使用python命令行创建客户端并调用服务:

>>> from suds.client import Client
>>> wsc = Client("http://localhost:8899/SOAP?wsdl")
>>> print wsc

Suds ( https://fedorahosted.org/suds/ )  version: 0.4 GA  build: R699-20100913

Service ( Application ) tns="tns"
   Prefixes (3)
      ns0 = "ResultInfo"
      ns1 = "TestRequestInfo"
      ns2 = "tns"
   Ports (1):
      (Application)
         Methods (1):
            getResultInfo(ns1:TestRequestInfo reqInfo, )
         Types (94):
            xs:ENTITIES
            xs:ENTITY
            xs:ID
            xs:IDREF
            ...
>>> requestInfo = {}
>>> requestInfo['reqNo'] = '12345'
>>> wsc.service.getResultInfo(requestInfo)
(ResultInfo){
   reqNo = "12345"
   resMsg = "Hello!"
 }
>>> 

总结

1、通过soaplib实现WebService服务端,通过suds自动实现客户端;
2、实际WebService接口中可能存在复杂对象数组,需要创建相应的ClassMdel类,并通过Array()实现。

参考资料

  1. 利用soaplib搭建webservice详细步骤和实例代码链接
  2. python开发webservice例子-服务端及客户端代码

相关文章