Python 開發與測試 Webservice(SOAP)


WebService是一種跨編程語言和跨操作系統平台的遠程調用技術。

理解WebService

1.從表面上看,WebService就是一個應用程序向外界暴露出一個能通過Web進行調用的API,也就是說能用編程的方法通過Web來調用這個應用程序。我們把調用這個WebService的應用程序叫做客戶端,而把提供這個WebService的應用程序叫做服務端。

2.從深層次看,WebService是建立可互操作的分布式應用程序的新平台,是一個平台,是一套標准。它定義了應用程序如何在Web上實現互操作性,你可以用任何你喜歡的語言,在任何你喜歡的平台上寫Web service ,只要我們可以通過Web service標准對這些服務進行查詢和訪問。 

Python 庫選擇

服務端開發:

針對Python的WebService開發,開發者討論最多的庫是soaplib(官方地址:http://soaplib.github.io/soaplib/2_0/index.html),但從其官網可知,其最新版本“soaplib-2.0.0-beta2”從2011年3月發布后就不再進行更新了。通過閱讀soaplib的官方文檔,可知其不再維護后已經轉向了一個新的項目:rpclib(官方地址:http://github.com/arskom/rpclib)進行后續開發,但在rpclib的readme中,介紹了rpclib已經更名為spyne,並將持續進行更新,so,那就選用spyne進行開發了。

spyne 官方文檔:http://spyne.io/docs/2.10/index.html

spyne github:https://github.com/arskom/spyne

  • spyne 安裝
pip install spyne
  • lxml 安裝:

下載與python匹配的版本安裝包  https://pypi.python.org/pypi/lxml/3.6.0 進行安裝,如 lxml-3.6.0.win-amd64-py2.7.exe (md5)

客戶端開發:

客戶端調用WebService一般應用suds庫。

使用參考文檔:https://fedorahosted.org/suds/wiki/Documentation

  • suds 安裝:
pip install suds

Spyne Introduction

Protocols:協議
  Protocols define the rules for transmission of structured data
Transports:傳輸
  Transports, also protocols themselves, encapsulate protocol data in their free-form data sections.
Models:模式
  Types like Unicode, Integer or ByteArray are all models. They reside in the spyne.model package.
Interface Documents:接口文檔
  Interface documents provide a machine-readable description of the expected input and output of the exposed method calls. 
Serializers:序列化對象
  Serializers are currently not distinguished in Spyne code. They are the protocol-specific representations of a serialized Python object.

How your code is wrapped?

step1:Your code is inside @rpc-wrapped methods in ServiceBase subclasses.

step2:The ServiceBase subclasses in turn are wrapped by an Application instance.

         The Application instantiation is used to assign input and output protocols to the exposed methods.

step3:The Application instance is finally wrapped by a client or server transport that takes the responsibility of moving the bits around.

step4:Deploying the service using Soap via Wsgi

服務端代碼實例(HelloWorld)

#!/usr/bin/env python 
# -*- coding: utf-8 -*-

"""
preference:
    http://spyne.io/docs/2.10/index.html
    https://github.com/arskom/spyne/blob/master/examples/helloworld_soap.py

This is a simple HelloWorld example to show the basics of writing
a webservice using spyne, starting a server, and creating a service
client.
Here's how to call it using suds:

#>>> from suds.client import Client
#>>> hello_client = Client('http://localhost:8000/?wsdl')
#>>> hello_client.service.say_hello('punk', 5)
(stringArray){
   string[] =
      "Hello, punk",
      "Hello, punk",
      "Hello, punk",
      "Hello, punk",
      "Hello, punk",
 }
#>>>

"""
# Application is the glue between one or more service definitions, interface and protocol choices.
from spyne import Application
# @rpc decorator exposes methods as remote procedure calls
# and declares the data types it accepts and returns
from spyne import rpc
# spyne.service.ServiceBase is the base class for all service definitions.
from spyne import ServiceBase
# The names of the needed types for implementing this service should be self-explanatory.
from spyne import Iterable, Integer, Unicode

from spyne.protocol.soap import Soap11
# Our server is going to use HTTP as transport, It’s going to wrap the Application instance.
from spyne.server.wsgi import WsgiApplication


# step1: Defining a Spyne Service
class HelloWorldService(ServiceBase):
    @rpc(Unicode, Integer, _returns=Iterable(Unicode))
    def say_hello(self, name, times):
        """Docstrings for service methods appear as documentation in the wsdl.
        <b>What fun!</b>
        @param name: the name to say hello to
        @param times: the number of times to say hello
        @return  When returning an iterable, you can use any type of python iterable. Here, we chose to use generators.
        """

        for i in range(times):
            yield u'Hello, %s' % name


# step2: Glue the service definition, input and output protocols
soap_app = Application([HelloWorldService], 'spyne.examples.hello.soap',
                       in_protocol=Soap11(validator='lxml'),
                       out_protocol=Soap11())

# step3: Wrap the Spyne application with its wsgi wrapper
wsgi_app = WsgiApplication(soap_app)

if __name__ == '__main__':
    import logging

    from wsgiref.simple_server import make_server

    # configure the python logger to show debugging output
    logging.basicConfig(level=logging.DEBUG)
    logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG)

    logging.info("listening to http://127.0.0.1:8000")
    logging.info("wsdl is at: http://localhost:8000/?wsdl")

    # step4:Deploying the service using Soap via Wsgi
    # register the WSGI application as the handler to the wsgi server, and run the http server
    server = make_server('127.0.0.1', 8000, wsgi_app)
    server.serve_forever()

服務端運行后,

訪問瀏覽器檢查服務  http://localhost:8000/?wsdl

瀏覽器中輸出wsdl文件:

 

 客戶端調用(代碼實例)

#!/usr/bin/env python 
# -*- coding: utf-8 -*-

from suds.client import Client  # 導入suds.client 模塊下的Client類

wsdl_url = "http://localhost:8000/?wsdl"


def say_hello_test(url, name, times):
    client = Client(url)                    # 創建一個webservice接口對象
    client.service.say_hello(name, times)   # 調用這個接口下的getMobileCodeInfo方法,並傳入參數
    req = str(client.last_sent())           # 保存請求報文,因為返回的是一個實例,所以要轉換成str
    response = str(client.last_received())  # 保存返回報文,返回的也是一個實例
    print req       # 打印請求報文
    print response  # 打印返回報文

if __name__ == '__main__':
    say_hello_test(wsdl_url, 'Milton', 2)

客戶端運行后,

查看客戶端控制台可見輸出:

 


***微信掃一掃,關注“python測試開發圈”,了解更多測試教程!***


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM