• python调用腾讯云短信接口


    python调用腾讯云短信接口

    账号注册

    1. 腾讯云官网注册一个腾讯云账号,通过实名认证

    2. 然后开通短信服务,创建短信应用,然后申请签名和模板

    python中封装腾讯云短信接口

    安装依赖

    pip install qcloudsms_py

    架构

    tx_msg
      |-- __init__.py
      |-- settings.py  # 配置
      |-- sms.py  # 实现发送短信接口
    

    settings.py

    # 短信应用 SDK AppID 以1400开头
    appid = xxxxx
    # 短信应用 SDK AppKey
    appkey = "xxxxxx"
    # 短信模板ID,需要在短信控制台中申请
    template_id = xxxxx
    # 签名 参数使用的是`签名内容`,而不是`签名ID`
    sms_sign = "xxxxx"
    

    sms.py

    from qcloudsms_py import SmsSingleSender
    from .settings import *
    import random
    
    # 短信发送成功的标识:发送后没有异常,响应的大字典中result值为0
    # {'result': 0, 'errmsg': 'OK', 'ext': '', 'sid': '2028:f826a20b647e9cfa4100', 'fee': 1}
    def send_sms(mobile, code, exp):
        ssender = SmsMultiSender(appid, appkey)
        params = [code, exc]
        mobile = (mobile, )
    
        try:
            response = ssender.send_with_param(86, mobile, template_id, params, sign=sms_sign, extend='', ext='')
            # print(response)
            detail = response['detail']
    
        except Exception as e:
            return False
    
        if detail[0].get('result') != 0:
            return False
    
        return True
    
    def get_code(num):
        """发送几位验证码"""
        code = ''
        for i in range(num):
            code += str(random.randint(0, 9))
        return code
    
  • 相关阅读:
    CF 1119 题解
    CF 582 题解
    CF 1098 题解
    CF 1129 题解
    CF 513 题解
    CF 417 D 题解
    ingress nginx遇到502错误,connect() failed (113 Host is unreachable) while connecting to upstream
    MySQL性能剖析
    MySQL的基准测试
    MySQL架构与历史
  • 原文地址:https://www.cnblogs.com/Hades123/p/11755460.html
Copyright © 2020-2023  润新知