python发送短信验证码

import ssl,json,random
from qcloudsms_py import SmsMultiSender, SmsSingleSender
from qcloudsms_py.httpclient import HTTPError

configInfo = json.load(open('config.json',encoding='utf-8'))
sms_appid = configInfo['sms']['appid']
sms_appkey = configInfo['sms']['appkey']
sms_sms_sign = configInfo['sms']['sms_sign']

#短信验证
def send_verify(phone):
    appid = sms_appid  # 自己应用ID
    appkey = sms_appkey  # 自己应用Key
    sign = sms_sms_sign  # 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称)
    sender = SmsSingleSender(appid, appkey)
    template_id = "877799"
    infoList = {}
    str = ""
    #六位随机数
    for i in range(6):
        ch = chr(random.randrange(ord('0'), ord('9') + 1))
        str += ch
    try:
        response = sender.send_with_param(86, phone, template_id, [str], sign=sign)
        infoList["code"] = 0
        infoList['data'] = response
        infoList['p_code'] = str
        infoList['msg'] = "短信发送成功"
    except Exception as e:
        response = {'result': 1000, 'errmsg': "网络异常发送失败"}
        infoList["code"] = 200
        infoList['data'] = response
        infoList['msg'] = "短信发送失败"
        infoList['error'] = e.args
    print(infoList)
    return infoList

#群发短信
def mass_texting(phone_list, template_id, param_list):
    appid = sms_appid  # 自己应用ID
    appkey = sms_appkey  # 自己应用Key
    sms_sign = sms_sms_sign  # 自己腾讯云创建签名时填写的签名内容(使用公众号的话这个值一般是公众号全称或简称)
    sender = SmsSingleSender(appid, appkey)
    try:
        response = sender.send_with_param(86, phone_list, template_id, param_list, sign=sms_sign)
    except HTTPError as e:
        response = {'result': 1000, 'errmsg': "网络异常发送失败"}
    return response
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值