python接口自动化41-suds测试webservice接口

前言

webservice 的接口,前面已经掌握了在 postman 上做接口测试,接下来使用 python 代码测试 webservice 接口

环境准备

先使用 pip 安装suds 库: pip install suds-jurko

>pip install suds-jurko
Collecting suds-jurko
  Downloading suds-jurko-0.6.zip (255 kB)
     |████████████████████████████████| 255 kB 6.2 kB/s
Using legacy 'setup.py install' for suds-jurko, since package 'wheel' is not installed.
Installing collected packages: suds-jurko
    Running setup.py install for suds-jurko ... done
Successfully installed suds-jurko-0.6

Client建立连接

创建websercive对象,返回全部方法,url是访问的webservice地址后面带上?wsdl

from suds import client

# webservice地址
url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"

# 创建webservice对象
cli = client.Client(url)
print(cli)  # 查看全部方法

结果返回

Suds ( https://fedorahosted.org/suds/ )  version: 0.6

Service ( MobileCodeWS ) tns="http://WebXml.com.cn/"
   Prefixes (1)
      ns0 = "http://WebXml.com.cn/"
   Ports (2):
      (MobileCodeWSSoap)
         Methods (2):
            getDatabaseInfo()
            getMobileCodeInfo(xs:string mobileCode, xs:string userID)
         Types (1):
            ArrayOfString
      (MobileCodeWSSoap12)
         Methods (2):
            getDatabaseInfo()
            getMobileCodeInfo(xs:string mobileCode, xs:string userID)
         Types (1):
            ArrayOfString

返回了2个方法getDatabaseInfo() 和 getMobileCodeInfo(xs:string mobileCode, xs:string userID)
通过浏览器访问也可以看到对应的方法和请求参数http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

调用service接口

先测试第一个接口: getDatabaseInfo
获得国内手机号码归属地数据库信息
输入参数:无;返回数据:一维字符串数组(省份 城市 记录数量)。

调用service接口的格式:cli.service.方法名称(参数)

from suds import client
# 作者-上海悠悠 QQ交流群:730246532
# blog地址 https://www.cnblogs.com/yoyoketang/


# webservice地址
url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"

# 创建webservice对象
cli = client.Client(url)
# print(cli)  # 查看全部方法

tels = cli.service.getDatabaseInfo()
print(tels)

于是可以返回结果

(ArrayOfString){
   string[] = 
      "全部 数据 265903",
      "安徽 安庆 658",
      "安徽 蚌埠 456",
      "安徽 亳州 489",
      "安徽 巢湖 323",
      "安徽 池州 281",
      "安徽 滁州 555",
      "安徽 阜阳 885",
      "安徽 合肥 1253",
      "安徽 淮北 310",
      "安徽 淮南 380",
      "安徽 黄山 256",
......
}

getMobileCodeInfo

获得国内手机号码归属地省份、地区和手机卡类型信息
输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 免费用户为空字符串;
返回数据:字符串(手机号码:省份 城市 手机卡类型)。

测试
若要使用 HTTP POST 协议对操作进行测试,请单击“调用”按钮。

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap:Body>
</soap:Envelope>

很明显这里有2个参数需要传,按上面文档的提示传对应的参数名称和值就行了

from suds import client
# 作者-上海悠悠 QQ交流群:730246532
# blog地址 https://www.cnblogs.com/yoyoketang/


# webservice地址
url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"

# 创建webservice对象
cli = client.Client(url)
print(cli)  # 查看全部方法

# 输入参数:mobileCode = 字符串(手机号码,最少前7位数字),
# userID = 字符串(商业用户ID) 免费用户为空字符串
result = cli.service.getMobileCodeInfo(mobileCode="152215501xx",
                                       userID="")
print(result)

结果返回:

152215501xx:上海 上海 上海移动全球通卡

使用 python 的 suds 库测试 webservice 其实更简单了,不用看那些繁琐的xml格式的数据了,根据接口文档传对应的参数即可。

posted @ 2021-02-20 23:41  上海-悠悠  阅读(1764)  评论(1编辑  收藏  举报