当前位置:网站首页>Flask sends verification code in combination with Ronglian cloud
Flask sends verification code in combination with Ronglian cloud
2022-07-29 00:36:00 【Wbig】
Send SMS verification code
1. Calmly take it out of the cloud accId,accToken,appId
After registering for ronglianyun , Find Management , Put the corresponding inside accId,accToken,appId Bring them out.
After taking it out, find an empty file and save it , For later use
2. stay Flask Install ronglianyun
Input... At the terminal
pip install ronglian_sms_sdk
Can be installed
3. Use Ronglian cloud
Create a tool folder under the root directory utils, Creating a SmsTools The file of
In the document , Instantiation , And use . For personal information ( for example :accId,accToken,appId) Take packaging treatment
import json,redis
from flask import current_app
from ronglian_sms_sdk import SmsSDK
class SmsTool():
''' Send SMS '''
def __init__(self):
accId = current_app.config.get("RL_ACC_ID")
accToken = current_app.config.get("RL_ACC_TOKEN")
appId = current_app.config.get("RL_APP_ID")
self.sdk = SmsSDK(accId, accToken, appId)
host=current_app.config.get("REDIS_HOST")
port = current_app.config.get("REDIS_PORT")
password = current_app.config.get("REDIS_PASSWORD")
self.rds = redis.Redis(host=host,port=port,password=password)
def send(self,phone,code):
''' Send SMS interface :param python: cell-phone number :param code: Verification Code :return: Bool Sending succeeded or failed '''
tid= current_app.config.get("RL_TID")
t = current_app.config.get("SMS_TIME")
print(t)
# Store the verification code in redis
key = "sms_%s"%phone
self.rds.set(key,code,ex= t * 60)
# Send a text message
datas=(code,t)
print(tid, phone, datas)
rs = self.sdk.sendMessage(tid,phone,datas) # return json character string
rs_data = json.loads(rs)
if rs_data["statusCode"] == "000000":
return True
return False
if __name__ == '__main__':
from app import app
with app.app_context():
s=SmsTool()
rs = s.send("18132317749","6666")
print(rs)
Create a file to encapsulate your personal information
class SecretConfig():
RL_ACC_ID = " Their own accid"
RL_ACC_TOKEN = " Their own acctoken"
RL_APP_ID = " Their own appid"
RL_TID = "1"
REDIS_HOST = "127.0.0.1"
REDIS_PORT = 6379
REDIS_PASSWORD = "123456"
4. Use a combination of
Import in the blueprint , Reference in blueprint , Instantiation
class SmsView(Resource):
def post(self):
req = reqparse.RequestParser()
req.add_argument("phone",required= True)
args = req.parse_args()
code = random.randint(100000,999999)
s = SmsTool()
rs = s.send(args["phone"],code)
if rs:
return jsonify({
"code": 200,
"msg": " Send successfully ",
})
return jsonify({
"code": 400,
"msg": " fail in send "
})
5. The practical application , SMS registration and login
def post(self):
# To obtain parameters
req = reqparse.RequestParser()
req.add_argument("phone",required=True)
req.add_argument("code",required=True)
# Calibration parameters
args = req.parse_args()
# Verification code
# Connect redis database
s = SmsTool()
# According to the cell phone number , Take out the corresponding verification code
key = "sms_%s" % args["phone"]
code_byte = s.rds.get(key) # The return value is byte type
if not code_byte:
return jsonify({
"code": 400,
"msg": " The verification code has expired "
})
# contrast
if args["code"] != code_byte.decode():
return jsonify({
"code": 400,
"msg": " Verification code error "
})
return jsonify({
"code": 200,
"msg": " Login successful "
})
边栏推荐
- Use hutool tool class to operate excel with more empty Sheet1
- Google browser, no installation required
- vulnhub:SolidState
- Attack and defense world web master advanced area PHP_ rce
- Upload Excel files with El upload and download the returned files
- Shell programming specifications and variables
- Talk about seven ways to realize asynchronous programming
- Soft test --- database (4) SQL statement
- 分布式限流 redission RRateLimiter 的使用及原理
- DCAT in laravel_ Admin preliminary use record
猜你喜欢

15.模型评估和选择问题

Nftscan and nftplay have reached strategic cooperation in the field of NFT data

There is a span tag. If you want to do click events on it, how can you expand the click area

Newscenter, advanced area of attack and defense world web masters

时间序列统计分析

Api 接口优化的那些技巧

How to solve the problems of MQ message loss, duplication and backlog?

Advanced area of attack and defense world web masters training www robots

requestVideoFrameCallback() 简单实例

Do like and in indexes in MySQL go
随机推荐
Shell编程规范与变量
SAP vl02n delivery note posting function WS_ DELIVERY_ UPDATE
Web系统常见安全漏洞介绍及解决方案-sql注入
【网络安全】通过iptables和ipset完成服务器防火墙黑名单和白名单功能
Dynamic programming problem (4)
动态规划问题(一)
聊聊异步编程的 7 种实现方式
Newscenter, advanced area of attack and defense world web masters
What does the expression > > 0 in JS mean
pnpm的安装与使用
flyway的快速入门教程
Advanced area of attack and defense world web masters -baby Web
MySQL 分库分表及其平滑扩容方案
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Dynamic programming problem (VII)
Longest ascending subsequence
The download file of native JS implementation can be used anywhere
How to solve the problem that the Oracle instance cannot be started
CDN mode uses vant components, and components cannot be called after they are introduced
【开发教程11】疯壳·开源蓝牙心率防水运动手环-整机功能代码讲解