python suds訪問webservice服務(wù)實現(xiàn)
安裝suds
在Python3環(huán)境下如果使用:pip install suds,應(yīng)該會報ImportError: No module named client,這里推薦安裝suds-py3。
使用
1.獲取所有方法
webservice中的方法,跟http中的get、post這種類似。
from suds.client import Client url = 'http://******************?wsdl' # wsdl地址 client = Client(url) print(client) # 查看定義的所有方法與請求所需攜帶的參數(shù)
返回的Methods中即定義的方法,包括請求所需攜帶的參數(shù)與參數(shù)類型。
2.調(diào)用方法
首先調(diào)用一個不帶參數(shù)的方法。
from suds.client import Client url = 'http://************************?wsdl' # wsdl地址 client = Client(url) response = client.service.getRealtimeDataList() # 返回列表,列表每一項是一個realtimeVo對象 for i in response: # 使用Client的dict方法,將realtimeVo對象轉(zhuǎn)換為dict print(Client.dict(i))
當調(diào)用需要傳入?yún)?shù)的方法時,在對應(yīng)方法內(nèi)直接按順序傳入就可以。
這里注意參數(shù)的類型,比如XML的dateTime類型,不能直接傳入python的datetime類型,會報錯的。這里需要用suds的DateTime轉(zhuǎn)換一下。具體代碼如下。
from suds.client import Client from suds.sax.date import DateTime from datetime import datetime, timedelta url = 'http://***************************?wsdl' # wsdl地址 client = Client(url) now = datetime.now() - timedelta(days=1) yesterday = now.strftime("%Y-%m-%d 00:00:00") # 返回字符串形式的日期 date_time = DateTime(yesterday) # DateTime既可以直接傳入字符串也可以直接傳入datetime對象,我這里傳入的字符串 response = client.service.getHistoryDataList(date_time, date_time, "address", "corpCode") # 返回列表,列表每一項是一個realtimeVo對象 for i in response: # 使用Client的dict方法,將realtimeVo對象轉(zhuǎn)換為dict print(Client.dict(i))
3.其他
其他方法,比如:
client.set_options() # 設(shè)置頭信息
目前本人沒用到過。
到此這篇關(guān)于python suds訪問webservice服務(wù)實現(xiàn)的文章就介紹到這了,更多相關(guān)python suds訪問webservice服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python連接Oracle之環(huán)境配置、實例代碼及報錯解決方法詳解
這篇文章主要介紹了Python連接Oracle之環(huán)境配置、實例代碼及報錯解決方法詳解,需要的朋友可以參考下2020-02-02Python讀取txt文件數(shù)據(jù)的方法(用于接口自動化參數(shù)化數(shù)據(jù))
這篇文章主要介紹了Python讀取txt文件數(shù)據(jù)的方法(用于接口自動化參數(shù)化數(shù)據(jù)),需要的朋友可以參考下2018-06-06