現如今面向服務(SOA)的架構設計已經成為主流,把公用的服務打包成一個個webservice供各方調用是一種非常常用的做法,而應用最廣泛的則是基於SOAP協議和wsdl的webservice。本文講解python環境下如何發布及調用一個基於SOAP的webservice,基於soaplib(發布)和suds(調用)。
OS:ubuntu 14.04 python:2.7.6
服務端:
1.安裝:
服務端要使用的工具包是soaplib,遺憾的是現在也停止維護了,不過好在還能用,下載地址在https://github.com/soaplib/soaplib,其官方文檔在http://soaplib.github.io/soaplib/2_0/
首先直接下載zip文件,解壓后直接運行python setup.py install即可。
2.寫一個webservice服務:
接下來舉個栗子,下面是一個接受一個string類型入參並返回一個string類型出參的webservice
1 import soaplib 2 from soaplib.core.model.primitive import String 3 from soaplib.core.server import wsgi 4 from soaplib.core.service import DefinitionBase #所有的服務類都繼承DefinitionBase基類 5 from soaplib.core.service import soap #soap標識方法的特性 6 7 from model import feedforward 8 9 10 class webserver(DefinitionBase): 11 @soap(String, _returns=String) 12 def GetModel(self, input): 13 return 'hello world' 14 15 if __name__ == '__main__': 16 try: 17 from wsgiref.simple_server import make_server 18 soap_application = soaplib.core.Application([webserver], 'tns', 'webservice') 19 wsgi_application = wsgi.Application(soap_application) 20 21 print "listening to http://0.0.0.0:7789" 22 print "wsdl is at: http://172.11.0.11:7789/?wsdl" 23 24 server = make_server('172.11.0.11', 7789, wsgi_application) 25 server.serve_forever() 26 27 except ImportError: 28 print "Error: example server code requires Python >= 2.5"
注意第11行的soap注解
@soap(String, _returns=String)
表示這個被注解的方法GetModel需要接受一個String類型的入參,_returns=String表示該方法返回的出參也是String類型。當然也可以設置更復雜的參數類型或是自定義類型,例如:
1 @soap(User, _returns=String) 2 def GetUser(self, user): 3 name = user.Name 4 return name
表示入參是一個自定義的類型User,出參為String,但是自定義的類型一定要繼承ClassModel類:
class User(ClassModel): __namespace__ = "User" Name=String
如果返回的類型是集合需要用soaplib的Array類型,例如:
@soap(_returns=Array(String)) def GetCdrArray(self): L_Result=["1","2","3"] #返回集合數據的格式 return L_Result
3.發布webservice
上例中從第15行if __name__ == '__main__'開始的就是webservice的發布過程。首先要創建一個Application對象:
soap_application = soaplib.core.Application([webserver], 'tns', 'webservice')
按照文檔,其中第一個參數表示 An iterable of ServiceBase subclasses that define the exposed services. 即把所有想要發布的服務所在的類裝入一個列表作為該方法的第一個參數
第二個參數表示 The targetNamespace attribute of the exposed service 即該webservice的命名空間,默認為tns
第三個參數表示 The name attribute of the exposed service 默認為None
接下來將soap_application轉化為一個wsgi_application: wsgi_application = wsgi.Application(soap_application)
最后新建一個server對象,設定IP和端口,接着啟動即可:
server = make_server('172.11.0.11', 7789, wsgi_application)
server.serve_forever()
這樣就發布了一個webservice,然后在瀏覽器里輸入http://172.11.0.11:7789/?wsdl就能看到相應的wsdl文件了
客戶端:
客戶端我們使用suds調用webservice
1.安裝
首先還是安裝,官方主頁https://fedorahosted.org/suds/,下載自己想要的版本,然后解壓,python setup.py install 即可,與安裝soaplib完全一致。文檔地址在https://fedorahosted.org/suds/wiki/Documentation
2.使用
使用suds調用webservice非常簡單。
import suds import logging logging.basicConfig(level=logging.INFO) class webservice_client(): def __init__(self, url): self.client = suds.client.Client(url) # 通過smile獲取model def get_model(self, input): try: model = self.client.service.GetModel(input) return model except Exception,e: logging.info('獲取model失敗:'+str(e)) return None if __name__ == '__main__': test = webservice_client('http://172.11.30.211:7789/?wsdl') print test.get_model('347349')
運行這個腳本,可以得到返回值hello world。
以上即python 發布及調用一個基於soap協議的wsdl類型webservice的方法