zoukankan      html  css  js  c++  java
  • 腾讯云短信接口完成验证码功能

     

     

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

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

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

    封装成一个包

     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
  • 相关阅读:
    php+redis简易消息队列
    Linux关闭selinux的方法(临时关闭和永久关闭)
    Linux清理buff/cache
    Centos禁止ping的设置方法
    浅谈mysql触发器
    mysql中left join right join inner join用法分析
    mysql主从配置详解(图文)
    mysql中的几种判断语句
    mysql锁表处理方法
    Mysql里的order by与索引
  • 原文地址:https://www.cnblogs.com/AbrahamChen/p/11782682.html
Copyright © 2011-2022 走看看