码迷,mamicode.com
首页 > 编程语言 > 详细

阿里云短信服务调用例子-Python

时间:2019-04-08 19:57:26      阅读:291      评论:0      收藏:0      [点我收藏+]

标签:request   http   cep   ict   dict   com   流控   短信   .com   

阿里云短信服务调用例子

阿里云官方文档https://helpcdn.aliyun.com/document_detail/101893.html

首先需要安装阿里云PythonSDK(下面是python3版本的安装方式)

pip install aliyun-python-sdk-core-v3

阿里云官方调用例子

技术图片
#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient(<accessKeyId>, <accessSecret>, default)

request = CommonRequest()
request.set_accept_format(json)
request.set_domain(dysmsapi.aliyuncs.com)
request.set_method(POST)
request.set_protocol_type(https) # https | http
request.set_version(2017-05-25)
request.set_action_name(SendSms)

request.add_query_param(PhoneNumbers, 15144445555)
request.add_query_param(SignName, xxxx)
request.add_query_param(TemplateCode, SMS_164561229)
request.add_query_param(TemplateParam, {"code": 100500})

response = client.do_action(request)
# python2:  print(response) 
print(str(response, encoding = utf-8))
View Code

我自己写的一个例子

import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest


class AliyunSms():
    def __init__(self, accessKeyId, accessSecret, signName):
        self.accessKeyId = accessKeyId
        self.accessSecret = accessSecret
        self.signName = signName

    def _generate_request(self, phone_num, code, templateCode):
        request = CommonRequest()
        request.set_accept_format(json)
        request.set_domain(dysmsapi.aliyuncs.com)
        request.set_method(POST)
        request.set_protocol_type(https)  # https | http
        request.set_version(2017-05-25)
        request.set_action_name(SendSms)
        request.add_query_param(PhoneNumbers, phone_num)
        request.add_query_param(SignName, self.signName)
        request.add_query_param(TemplateCode, templateCode)
        request.add_query_param(TemplateParam, {"code":  + code + })
        return request

    def _generate_client(self):
        client = AcsClient(self.accessKeyId, self.accessSecret, default)
        return client

    def send_sms(self, phone_num, code, templateCode):
        """
        发送短信验证码,返回Code字段的值
        :param phone_num: 手机号
        :param code: 验证码内容
        :param templateCode: 验证码模板
        :return:
        """
        client = self._generate_client()
        request = self._generate_request(phone_num, code, templateCode)
        try:
            response = client.do_action(request)  # bytes 类型
            response = response.decode()  # str类型
            response_dict = json.loads(response)  # {‘Message‘: ‘触发天级流控Permits:1‘, ‘RequestId‘: ‘5FA848EB-7C84-469D-8254-043835A05624‘, ‘Code‘: ‘isv.BUSINESS_LIMIT_CONTROL‘}
            return response_dict[Code]

        except Exception as e:
            return None

if __name__ == __main__:
    ali = AliyunSms(这里填accessKeyId, 这里填accessKeySecret, 这里填签名名称)
    a = ali.send_sms(13067742856, 这里是验证码内容,  这里写模版CODE)
    print(a)

模板CODE在这里

技术图片

签名名称在这里

技术图片

 

阿里云短信服务调用例子-Python

标签:request   http   cep   ict   dict   com   流控   短信   .com   

原文地址:https://www.cnblogs.com/blog-rui/p/10672898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!