当前位置:网站首页>运用flask框架发送短信验证码的流程及具体代码
运用flask框架发送短信验证码的流程及具体代码
2022-07-31 05:09:00 【城南花开了^】
1.获取短信验证码流程图

2.容联云配置
在容联云官网注册一个账号,发送短信验证码必须使用三个ID
ACCOUNT SID (主账户ID)
AUTH TOKEN (账户授权令牌)
AppID (AppID)

3.添加测试手机号 ( 必填 )

4.在flask后端安装容联云SDK
pip install ronglian_sms_sdk
发送短信调用配置
import json
from ronglian_sms_sdk import SmsSDK
def send_message(sms_code,mobile,expire=5):
sms_sdk = SmsSDK(accId='8aaf03013***********************',
appId='8aaf07087f77bf96017fddbf5f3e3334',
accToken='b2033c3b6d3b4dd6b931a138a8246af1')
tid = '1'
datas = ("%s"%sms_code,"%s"%expire)
res = sms_sdk.sendMessage(tid=tid,mobile=mobile,datas=datas)
resd=json.loads(res)
return resd
发送验证码视图
这里是将手机号作为存入redis时的key使用
import redis
from flask import jsonify
from flask_restful import Resource,reqparse
# 生成并存储短信验证码
class Scode(Resource):
def get(self):
paser = reqparse.RequestParser()
paser.add_argument('mobile')
args = paser.parse_args()
mobile = args.get('mobile')
sms_code = random.randint(100000,999999)
send_message(sms_code,mobile)
# 将验证码存入redis中
r = redis.Redis(host='localhost',db=0,port=6379)
sms_key = "sms_code:%s"%mobile
r.set(sms_key,sms_code,ex=300)
print("<<<验证码>>>",sms_code)
return jsonify(msg = "验证码已发送,请注意查收",code=200)
触发获取验证码函数即可发送成功
————————————————
边栏推荐
- Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
- 【LeetCode-SQL每日一练】——2. 第二高的薪水
- Minio上传文件ssl证书不受信任
- MySQL常见面试题汇总(建议收藏!!!)
- Paginate the list collection and display the data on the page
- centos7安装mysql5.7步骤(图解版)
- DVWA安装教程(懂你的不懂·详细)
- pytorch中的一维、二维、三维卷积操作
- Minesweeper game (written in c language)
- 城市内涝及桥洞隧道积水在线监测系统
猜你喜欢
随机推荐
matlab abel变换图片处理
centos7安装mysql5.7步骤(图解版)
分布式事务处理方案大 PK!
如何将项目部署到服务器上(全套教程)
为什么要用Flink,怎么入门使用Flink?
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
Linux的mysql报ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘ (using password NOYSE)
Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
MySQL-Explain详解
MySQL优化:从十几秒优化到三百毫秒
【LeetCode-SQL每日一练】——2. 第二高的薪水
Temporal线上部署
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
a different object with the same identifier value was already associated with the session
MySQL事务隔离级别详解
Summary of MySQL common interview questions (recommended collection!!!)
【一起学Rust】Rust学习前准备——注释和格式化输出
[Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
MySQL forgot password









