如果要用python测试http接口,需要requests这个库来实现对http接口的测试,那么,针对webservice接口,python如何实现,肯定是需要安装第三方的库来实现,需要安装第三方库suds
suds的安装
在windows中的dos窗口中执行pip install suds命令
python测试webservice接口案例
首先需要导入suds中的client的包:
from suds.client import Client
from suds.client import Client
#定义client对象
url="http://ws.webxml.com.cn/webservices/MobileCodeWS.asmx?wsdl"
client =Client(url)
print(client)
以下显示打印client显示的信息,会显示接口中的方法
1.接口请求中无参数案例
#请求接口,接口请求无参数
res=client.service.getDatabaseInfo()
#显示接口请求后的返回信息
print(res)
#getDatabaseInfo为方法名
2.接口请求中有参数案例
#接口请求中,有参数
res=client.service.getMobileCodeInfo(mobileCode='13111111111')
print(res)
3.请求中有复杂参数的接口案例
#接口请求中,有复杂参数的接口,需要使用factory.create进行构造请求参数
res=client.factory.create("ns0:sendCodeParams")#传入的是请求方法中的参数
print(res)#以下显示参数信息
"""
(sendCodeParams){
mobile = None
(tmpl_value){
entry[] = <empty>
}
use = None
}
"""
#组装参数
data=dict(
mobile = None,
tmpl_value = {},
use = None
)
#请求接口
response=client.service.sendMCode(data)
#在控制台打印接口返回信息
print(response)