发送短信验证码

利用云片网发送短信验证码:

# 1. 在云片网后台申请 签名 和 模板

# 2. 在 系统设置 中添加 IP白名单 (服务器IP或者你自己本地的IP地址)

# 3. 选择 单条发送,查看接口使用说明

# 访问方式: POST,接口支持 https 和 http; URL为:https://sms.yunpian.com/v2/sms/single_send.json

接口代码示例:

import json
import requests

class YuanPianVerifyCode(object):
    def __init__(self,api_key):
        self.api_key = api_key
        self.send_single_url = "https://sms.yunpian.com/v2/sms/single_send.json"
        
    def send_single_msg(self,code,mobile):  # code :验证码; mobile:手机号
        params = {
            "apikey":self.api_key,
            "mobile": mobile,
            "text":"【跨境电商独立站】您的验证码是{code}。如非本人操作,请忽略本短信。".format(code=code)   # "text"的设置决定了发送成功与否; "text" 的内容应该和 模板中的内容一致
        }
        response = requests.post(self.send_single_url,data=params)
        res_dict = json.loads(response.text)  # 对 response.text 序列化
     return res_dict
""" 调用该接口时,先用 api_key 实例化一个 YuanPianVerifyCode 的对象,然后用这个对象调用 send_single_msg() 方法,如: yuan_pian = YuanPianVerifyCode("apikey") res = yuan_pian.send_single_msg("20181024","131812345678") # 验证码需要自己后端生成 # res是一个字典,里面有 "code" 和 "msg" 这两个key; res["code"]==0 表示发送成功
"""
原文地址:https://www.cnblogs.com/neozheng/p/9846862.html