Python 腾讯云短信,发送手机验证码

1.安装包

pip install qcloudsms_py

2.准备好相关参数

腾讯云短信每个月赠送100条短信,申请一个应用,获取appid,然后创建短信签名,然后创建正文模版

3.发送短信

我们使用70439这个这个模版来发送短信

这个模版里只有一个参数{1},所以发送的时候就写一个参数

#coding:utf-8

from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError

appid = 140005xxxx
appkey = "4fcd263d315340bf578c93de89faxxxx"
phone_numbers = ["1520284xxxx", "1814439xxxx"] #手机号可以添加多个多个
template_id = 74039  


ssender = SmsSingleSender(appid, appkey)
params = ["1234",]  #发送验证码为1234
try:
    result = ssender.send_with_param(86, phone_numbers[0],
        template_id, params)
except HTTPError as e:
    print(e)
except Exception as e:
    print(e)

print(result)

4,成功返回事例

{u'ext': u'', u'fee': 1, u'result': 0, u'errmsg': u'OK', u'sid': u'8:3xujxW6UPZcVeP6hZfr20180106'}

注:如果发送模版有多个参数就需要在代码中传入多个参数。

例如:

您的验证码为{1},{2}分钟内有效。


params = ["1234","30"]


效果为你的验证码为1234,30分钟内有效
 
原文地址:https://www.cnblogs.com/fanhua999/p/8214786.html