• 阿里云短信发送服务SDK-Python3


    本文提供阿里云的短信发送服务SDK,使用Python3实现。

    # -*- coding: utf-8 -*-
    # pip install requests
    import requests
    import urllib.request
    import time
    import uuid
    import hmac
    import base64
    import datetime
    import json
    
    def params(accesskeyid, mobiles, tpl_code, tpl_params, sign_name):
        p = [
            ["SignatureMethod", "HMAC-SHA1"],
            ["SignatureNonce", uuid.uuid4().hex],
            ["AccessKeyId", accesskeyid],
            ["SignatureVersion", "1.0"],
            ["Timestamp", time_now_fmt()],
            ["Format", "JSON"],
    
            ["Action", "SendSms"],
            ["Version", "2017-05-25"],
            ["RegionId", "cn-hangzhou"],
            ["PhoneNumbers", "{0}".format(mobiles)],
            ["SignName", sign_name],
            ["TemplateParam", json.dumps(tpl_params, ensure_ascii=False)],
            ["TemplateCode", tpl_code],
            ["OutId", "123"],
        ]
        return p
    
    
    def time_now_fmt():
        r = datetime.datetime.utcfromtimestamp(time.time())
        r = time.strftime("%Y-%m-%dT%H:%M:%SZ", r.timetuple())
        return r
    
    
    def special_url_encode(s):
        r = urllib.parse.quote_plus(s).replace("+", "%20").replace("*", "%2A").replace("%7E", "~")
        return r
    
    
    def encode_params(lst):
        s = "&".join(list(map(
            lambda p: "=".join([special_url_encode(p[0]), special_url_encode(p[1])]),
            sorted(lst, key=lambda p: p[0])
        )))
        return s
    
    
    def prepare_sign(s):
        r = "&".join(["GET", special_url_encode("/"), special_url_encode(s)])
        return r
    
    
    def sign(access_secret,  prepare_str):
        k = "{0}{1}".format(access_secret, "&")
        r = hmac.new(k.encode(), prepare_str.encode(), sha1).digest()
        base_str = base64.b64encode(r).decode()
        return special_url_encode(base_str)
    
    
    def _send_sms_ali(mobiles, tpl_code, tpl_params):
        prefix_url = "https://dysmsapi.aliyuncs.com/?"
    
        accesskeyid = "xxxxx"
        accesssecret = "xxxxx"
        sign_name = "xxxxx"
    
        params_lst = params(accesskeyid, mobiles, tpl_code, tpl_params, sign_name)
        eps = encode_params(params_lst)
        prepare_str = prepare_sign(eps)
        sign_str = sign(accesssecret, prepare_str)
    
        url = "{0}Signature={1}&{2}".format(prefix_url, sign_str, eps)
    
        r = requests.get(url)
        if r.status_code != 200:
            return False
        else:
            jn = json.loads(r.text)
            if jn.get("Code") == "OK":
                return True
            else:
                return False
    
    
    if __name__ == "__main__":
        # 签名校验测试,与测试样例一致,待拿到正式参数时再做测试修改
    
        _tpl_code = "SMS_120785003"
        _tpl_params = {"code": "0000"}
        _send_sms_ali("150xxxxxxxxx,177xxxxxxxx", _tpl_code, _tpl_params)
    
    
  • 相关阅读:
    access导入报错 请求筛选模块被配置为拒绝超过请求内容长度的请求
    win10装回win7。PE下把原来的系统盘格掉,再安装hdd,重启就好了
    sql语句Order by 报错列名不明确
    C#字符串长度判断
    Struts2中数据封装机制
    Struts2中的页面跳转
    Struts2访问Servlet API的三种方式
    Struts2的动态Action实现
    Struts2基本概念
    javaweb开发之EL表达式
  • 原文地址:https://www.cnblogs.com/bitquant/p/alisms-sdk-python3.html
Copyright © 2020-2023  润新知