* coding:utf-8 *
@time 2020/5/2 11:28
@Author xiaoxiao
@name webservice.py
import suds
from suds import client
webservice接口地址:需要注意的是接口地址返回的是一个xml文件。既可以用来承载数据,也可以直接作为文档。
想要直观查看接口文档,可以去掉"?wsdl",就可以直观看到有哪些接口。以及参数信息
url = “http://www.webxml.com.cn/WebServices/ipAddressSearchWebService.asmx?wsdl”
定义一对象/实例来接受返回的webservice的Client对象
cil = client.Client(url=url)
查看返回的数据类型----是一个对象(类)
print(type(cil))
查看这个对象下面有哪些接口
print(“返回的结果:”, cil)
“”"
Suds ( https://fedorahosted.org/suds/ ) version: 0.6
Service ( IpAddressSearchWebService ) tns=“http://WebXml.com.cn/”
Prefixes (1)
ns0 = “http://WebXml.com.cn/”
Ports (2):
(IpAddressSearchWebServiceSoap)
Methods (3):
getCountryCityByIp(xs:string theIpAddress)
getGeoIPContext()
getVersionTime()
Types (1):
ArrayOfString
(IpAddressSearchWebServiceSoap12)
Methods (3):
getCountryCityByIp(xs:string theIpAddress)
getGeoIPContext()
getVersionTime()
Types (1):
ArrayOfString
“”"
选择要请求的结构。发送参数请求,注意发送请求是通过client对象里面的service方法来发送!
try:
# 这里是单个参数的情况。
res = cil.service.getCountryCityByIp(“1.125.1.1”)
print(“请求结果:”, res)
捕获异常信息
except suds.WebFault as e:
print(“服务器返回的错误信息;”, e.fault)
“”"
请求结果: (ArrayOfString){
string[] =
“1.125.1.1”,
"澳大利亚 ",
}
“”"
扩展说明
“”"
如果参数是多个时,需要使用字典的形式
“”"
用字典的方式传值
params_dict = {“msgCode”: “2”, “sysCode”: “111”}
try:
# 这里是单个参数的情况。
res = cil.service.getCountryCityByIp(params_dict)
print(“请求结果:”, res)
捕获异常信息
except suds.WebFault as e:
print(“服务器返回的错误信息;”, e.fault)