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

Posted Jary

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云短信服务调用例子-Python相关的知识,希望对你有一定的参考价值。

阿里云短信服务调用例子

阿里云官方文档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的主要内容,如果未能解决你的问题,请参考以下文章

各位大神,Python怎么调用阿里云API

java对接阿里云短信服务器,附代码和截图

各位大神,Python怎么调用阿里云API

[python][工具]阿里云平台短信验证功能

[python][工具]阿里云平台短信验证功能

阿里云对象存储调用例子