当前位置: 首页 > 后端技术 > Python

pythonsuds访问webservice服务

时间:2023-03-26 16:18:15 Python

installsuds如果在Python3环境下使用:pipinstallsuds,应该会报ImportError:Nomodulenamedclient,这里建议安装suds-py3。使用1.获取webservice中的所有方法,类似于http中的get和post。fromsuds.clientimportClienturl='http://******************?wsdl'#wsdl地址client=Client(url)print(client)#查看定义的所有方法请求需要的参数和参数是返回的Methods中定义的方法,包括请求需要的参数和参数类型。2.调用一个方法首先调用一个没有参数的方法。fromsuds.clientimportClienturl='http://************************?wsdl'#wsdl地址client=Client(url)response=client.service.getRealtimeDataList()#返回一个列表,列表中的每一项都是一个realtimeVo对象foriinresponse:#使用Client的dict方法将realtimeVo对象转换成dictprint(Client.dict(i))needs调用时传入参数的方法时,直接在相应的方法中按顺序传入即可。注意这里参数的类型,比如XML的datetime类型,不能直接传入python的datetime类型,会报错。这里需要使用suds的DateTime进行转换。具体代码如下。从suds.clientimportClientfromsuds.sax.dateimportDateTimefromdatetimeimportdatetime,timedeltaurl='http://*****************************?wsdl'#wsdl地址client=Client(url)now=datetime.now()-timedelta(days=1)yesterday=now.strftime("%Y-%m-%d00:00:00")#以字符串的形式返回日期date_time=DateTime(yesterday)#DateTime可以直接作为字符串或者datetime对象传入。我这里传入的字符串是response=client.service.getHistoryDataList(date_time,date_time,"address","corpCode")#返回一个列表,列表中的每一项都是一个realtimeVo对象foriinresponse:#使用Client的dict方法把realtimeVo对象转成dictprint(Client.dict(i))3.其他方法,比如:client.set_options()#我没用过它来设置header信息。