• 腾讯云短信接口



    如何快速开通腾讯云短信服务:https://cloud.tencent.com/document/product/382/37745

    使用python进行配置的间接:https://cloud.tencent.com/document/product/382/11672

    个人使用心得(下面的例子是我个人项目中使用的)

    封装成一个包

    img

    settings.py

    # 短信应用 SDK AppID
    APPID = 1400009099  # SDK AppID 以1400开头
    # 短信应用 SDK AppKey
    APPKEY = "9ff91d87c2cd7cd0ea762f141975d1df37481d48700d70ac37470aefc60f9bad"
    # 短信模板ID,需要在短信控制台中申请
    TEMPLATE_ID= 7839  # NOTE: 这里的模板 ID`7839`只是示例,真实的模板 ID 需要在短信控制台中申请
    # 签名
    SMS_SIGN = "腾讯云"  # NOTE: 签名参数使用的是`签名内容`,而不是`签名ID`。这里的签名"腾讯云"只是示例,真实的签名需要在短信控制台中申请
    

    sms.py

    from qcloudsms_py import SmsSingleSender
    from .settings import *
    from luffyapi.utils.logging import logger
    import random
    #mac系统的ssl安全验证
    import ssl
    ssl._create_default_https_context = ssl._create_unverified_context
    
    
    sender = SmsSingleSender(APPID,APPKEY)
    class Send_sms:
    
        def __init__(self,mobile,exp):
            self.mobile = mobile
            self.code = self.get_code()
            self.exp = exp
        
        #  短信发送模块
        def send_sms(self):
            try:
                response = sender.send_with_param(86, self.mobile, TEMPLATE_ID, (self.code, self.exp), sign=SMS_SIGN, extend="", ext="")
            except Exception as e:
                logger.error('sms error: %s' % e)
                return False
            if response and response['result'] == 0:
                return True
            logger.error('sms error:%s' % response['errmsg'])
            return False
        
        #  随机验证码生成模块
        def get_code(self):
            self.code = ''
            for i in range(4):
                self.code += str(random.randint(0, 9))
            return self.code
    

    init.py

    from .sms import Send_sms
    

    提醒:

    qcloudsms_py模块别忘记安装了,指令如下

    pip install qcloudsms_py
    
    我把月亮戳到天上 天就是我的 我把脚踩入地里 地就是我的 我亲吻你 你就是我的
  • 相关阅读:
    什么是Netflix Feign?它的优点是什么?
    Spring Boot 自动配置原理是什么?
    springcloud断路器作用?
    什么是SpringCloudConfig?
    find命令查找包含指定内容的文件
    @PostConstruct使用总结
    @Retention 注解的作用
    SpringBoot自定义Condition注解
    Spring Boot 入门
    SpringBoot +MSSQL
  • 原文地址:https://www.cnblogs.com/zhulipeng-1998/p/12863828.html
Copyright © 2020-2023  润新知