本文提供阿里云的短信发送服务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],
        ["],
    ]
    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 = {"}
    _send_sms_ali("150xxxxxxxxx,177xxxxxxxx", _tpl_code, _tpl_params)

阿里云短信发送服务SDK-Python3的更多相关文章

  1. Abp 添加阿里云短信发送

    ABP中有短信发送接口ISmsSender public interface ISmsSender { Task<string> SendAsync(string number, stri ...

  2. 2018阿里云短信发送DEMO接入简单实例

    以下更新2018-04-2309:57:54 后续不再更新, 基本类: app/SignatureHelper.php <?php namespace aliyun_mns; /** * 签名助 ...

  3. spring boot集成阿里云短信发送接收短信回复功能

    1.集成阿里云通信发送短信: 在pom.xml文件里添加依赖 <!--阿里短信服务--> <dependency> <groupId>com.aliyun</ ...

  4. Java调用阿里云短信通道服务【千锋】

    这里我们使用SpringBoot 来调用阿里通信的服务. 阿里通信,双11.收到短信,日发送达6亿条.保障力度非常高. 使用的步骤: 1.1. 第一步:需要开通账户 1.2. 第二步:阅读接口文档 1 ...

  5. 零基础大数据入门教程:Java调用阿里云短信通道服务

    这里我们使用SpringBoot 来调用阿里通信的服务. 阿里通信,双11.收到短信,日发送达6亿条.保障力度非常高. 使用的步骤: 1.1. 第一步:需要开通账户 1.2. 第二步:阅读接口文档 1 ...

  6. .net core 使用阿里云短信发送SMS

    阿里云官方的skd(aliyun-net-sdk-core,aliyun-net-sdk-dysmsapi)在dnc中发送短信会出错,nuget上的包貌似也一样不管用.直接改下sdk当然也可以,但就发 ...

  7. tp5阿里云短信发送

    到阿里云下载php版demo,下完整版的,不是轻量级的; 框架  :TP5 把下载下来的文件放到extend里面 文件名:alimsg 里面的文件 import('alimsg.api_demo.Sm ...

  8. java 阿里云短信发送

    记录自己的足迹,学习的路很长,一直在走着呢~ 第一步登录阿里云的控制台,找到此处: 点击之后就到此页面,如果发现账号有异常或者泄露什么,可以禁用或者删除  AccessKey: 此处方便测试,所以就新 ...

  9. 移动端获取短信验证码java实现——阿里云短信服务

    需求:移动端输入手机号,获取验证码.点击登录,验证验证码是否输入错误.是否超时等情况,一旦校验通过,将用户数据保存到数据中(业务逻辑). 前提:注册阿里用户,开通短信服务,申请key.秘钥.签名.短信 ...

随机推荐

  1. mac 下搭建Elasticsearch 5.4.3分布式集群

    一.集群角色 多机集群中的节点可以分为master nodes和data nodes,在配置文件中使用Zen发现(Zen discovery)机制来管理不同节点.Zen发现是ES自带的默认发现机制,使 ...

  2. patrol_data_unit_edit.jsp

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ page language ...

  3. ngingx安装错误 ./configure: error: the HTTP rewrite module requires the PCRE library.

    有时候,我们需要单独安装nginx,来处理大量的下载请求.单独在Centos5安装nginx遇到的rewrite和HTTP  cache错误解决办法: wget http://nginx.org/do ...

  4. cxGrid数据录入

    一.数据录入 1 在TcxGridDBTableView中,设定属性 NewItemRow.Visible = True 2 在cxgrid中输入数据怎样回车换行   在TcxGridDBTableV ...

  5. 使用virtualbox 配置 linux host-only虚拟主机连接外网(转载)

    host-only 下的虚拟机之间可以互相访问,虚拟机和宿主机可以互相访问,但是虚拟机不能访问外网. 需要设置: 1.宿主机设置 先对宿主机(windows机器,我这里是win7系统)进行相关配置. ...

  6. 九度OJ小结2

    由于安排问题,距离上次小结时间已经过去很久.导致这次小结的内容很多. 本次小结涉及到主要内容如下所示: 基于并查集操作的最小生成树问题(prime算法或者kruskal算法): 最短路径问题(Floy ...

  7. 题目1454:Piggy-Bank(完全背包问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1454 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  8. 题目1460:Oil Deposit(递归遍历图)

    题目链接:http://ac.jobdu.com/problem.php?pid=1460 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  9. 使用maven项目 转XSD、 WSDL 成Java类

    因为项目需要,在网上查找了很多WSDL的使用方式,但是感觉不是特别顺利.现在是利用Maven 项目快速转化的方法. 首先建立一个maven 项目 ,并将下面的pom文件复制进去. <projec ...

  10. css零零散散的笔记

    1.div根据内容自适应大小 效果图: html: <body> <div class="parent"> <div class="chil ...