以前使用過(guò)Java和Perl編寫(xiě)SOAP服務(wù),還比較簡(jiǎn)單的,對(duì)應(yīng)的庫(kù)有Axis和SoapLite。最近由于一個(gè)項(xiàng)目大部分使用Python作為開(kāi)發(fā)語(yǔ)言,考慮使用Python來(lái)編寫(xiě)SOAP對(duì)外提供服務(wù)。眾所周知,Python的動(dòng)態(tài)和簡(jiǎn)單是出了名的,用它編寫(xiě)SOAP服務(wù)也相當(dāng)簡(jiǎn)單的,要比Java和Perl來(lái)得更輕松。
使用Python編寫(xiě)SOAP,可以下載SOAPPy庫(kù)。
編寫(xiě)SOAP服務(wù),用于獲得服務(wù)器當(dāng)前的時(shí)間:
import sys sys.path.insert (1, ‘..‘) from SOAPpy import * import time def gettime(): return time.strftime(‘%Y-%m-%D %H:%M:%S‘, time.localtime()) namespace = ‘http:///‘ server = SOAPServer (("localhost", 9000)) server.registerKWFunction (gettime, namespace) try: while True: server.handle_request() except KeyboardInterrupt: pass
以下是訪問(wèn)該SOAP服務(wù)的客戶(hù)端測(cè)試代碼:
import sys sys.path.insert (1, ‘..‘) from SOAPpy import * endpoint = "http://localhost:9000/" ns = "http:///" serv = SOAPProxy(endpoint, namespace=ns) print serv.gettime()
與其他語(yǔ)言相比,這已經(jīng)簡(jiǎn)化了許多,而且可以在不用編寫(xiě)本地代碼的情況下就直接調(diào)用SOAP服務(wù)的方法,這和訪問(wèn)本地服務(wù)一樣,非常的輕松。現(xiàn)在唯一擔(dān)心的是它的性能,因?yàn)楹?jiǎn)單一定會(huì)帶來(lái)其他的問(wèn)題。但對(duì)于目前而言,可以暫時(shí)不用考慮這個(gè)。




