soap方式调用webserver接口发送短信

1、前言
使用soap协议去调用webserver的接口来实现发送短信的功能。
简单的了解了下发现这是基于xml的,发现貌似不会这玩意呀,以为会有点小复杂来开始踩坑。
2、安装包
pip install suds-jurko
# 注意py版本不同的话,这个包可能有差异。我这测试的py版本是3.6.8
3、找webserver接口地址
"http://xxx.xxx.xxx.xxx/msgws/service.asmx?wsdl"

# 找你需要的接口,都是wsdl形式的,或者让第三方提供
4、具体实现
import http.client as client
import time
from suds.transport.https import HttpAuthenticated

from suds.client import Client


class SendMassage(object):
    def __init__(self, username, password, mobile, context):
        self._username = username
        self._password = password
        self._mobile = mobile
        self._context = context

    def get_current_timestamp(self):
        return int(round(time.time() * 1000))

    def suds_client(self):
        sms_url = "http://xxx.xxx.xxx.xxx/msgws/service.asmx?wsdl"
        auth = HttpAuthenticated(username=self._username, password=self._password)
        try:
            client = Client(sms_url, transport=auth)
            print(client)
						"""
						client 里可以查看有什么方法和类型
						"""
            
            request = client.factory.create('MsgSend')  # 初始化方法
            # 传参数
            request.YHM = self._username
            request.MM = self._password
            request.DestMobile= self._mobile
            request.MsgContent = self._context
            response = client.service.MsgSend(request)
            print(response)

        except Exception as e:
            print(e)
posted @ 2022-05-09 14:22  Lei、Sunny  阅读(200)  评论(0编辑  收藏  举报