当前位置:网站首页>运用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)
触发获取验证码函数即可发送成功
————————————————
边栏推荐
- pytorch中的一维、二维、三维卷积操作
- Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
- 【LeetCode-SQL每日一练】——2. 第二高的薪水
- 【ORACLE Explain 详解】
- ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
- Unity Framework Design Series: How Unity Designs Network Frameworks
- MySQL8--Windows下使用压缩包安装的方法
- datagrip带参sql查询
- Sql解析转换之JSqlParse完整介绍
- 再见了繁琐的Excel,掌握数据分析处理技术就靠它了
猜你喜欢

Unity Framework Design Series: How Unity Designs Network Frameworks

面试官竟然问我怎么分库分表?幸亏我总结了一套八股文

【mysql 提高查询效率】Mysql 数据库查询好慢问题解决

TOGAF之架构标准规范(一)

Refinement of the four major collection frameworks: Summary of List core knowledge

工作流编排引擎-Temporal

Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work

ERP Production Operation Control Kingdee

MySQL开窗函数

MySQL-如何分库分表?一看就懂
随机推荐
DVWA installation tutorial (understand what you don't understand · in detail)
sql语句之多表查询
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
分布式事务处理方案大 PK!
质量小议12 -- 以测代评
Doris学习笔记之监控
[C language] Detailed explanation of operators
numpy和pytorch中的元素拼接操作:stack,concatenat,cat
面试官,不要再问我三次握手和四次挥手
MySQL database installation (detailed)
城市内涝及桥洞隧道积水在线监测系统
Anaconda配置环境指令
Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
Unity mobile game performance optimization series: performance tuning for the CPU side
torch.normal函数用法
可点击也可直接复制指定内容js
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
Temporal对比Cadence
【一起学Rust】Rust学习前准备——注释和格式化输出
MYSQL下载及安装完整教程