python suds訪問webservice服務實現
安裝suds
在Python3環境下如果使用:pip install suds,應該會報ImportError: No module named client,這里推薦安裝suds-py3。
使用
1.獲取所有方法
webservice中的方法,跟http中的get、post這種類似。
from suds.client import Clienturl = ’http://******************?wsdl’ # wsdl地址client = Client(url)print(client) # 查看定義的所有方法與請求所需攜帶的參數
返回的Methods中即定義的方法,包括請求所需攜帶的參數與參數類型。
2.調用方法
首先調用一個不帶參數的方法。
from suds.client import Clienturl = ’http://************************?wsdl’ # wsdl地址client = Client(url)response = client.service.getRealtimeDataList() # 返回列表,列表每一項是一個realtimeVo對象for i in response: # 使用Client的dict方法,將realtimeVo對象轉換為dict print(Client.dict(i))
當調用需要傳入參數的方法時,在對應方法內直接按順序傳入就可以。
這里注意參數的類型,比如XML的dateTime類型,不能直接傳入python的datetime類型,會報錯的。這里需要用suds的DateTime轉換一下。具體代碼如下。
from suds.client import Clientfrom suds.sax.date import DateTimefrom datetime import datetime, timedeltaurl = ’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對象轉換為dict print(Client.dict(i))
3.其他
其他方法,比如:
client.set_options() # 設置頭信息
目前本人沒用到過。
到此這篇關于python suds訪問webservice服務實現的文章就介紹到這了,更多相關python suds訪問webservice服務內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: