当前位置:网站首页>Send SMS verification code synchronously
Send SMS verification code synchronously
2022-07-23 11:16:00 【ZXY_ lucky】
1. Register Ronglian cloud
Rong Lianyun address :https://www.yuntongxun.com/
After successful registration, copy and save the account information
2. Send SMS synchronously
2.1 install SDK
pip install ronglian_sms_sdk
2.2 Send SMS to call
Under sub application utils.py
from ronglian_sms_sdk import SmsSDK
import json
accId = "8aaf07088185853e01818a75a87a016f"
accToken = "6b755e025dc2436a8e3b7b82ef9ac20c"
appId = "8aaf07088185853e01818a75a9a00176"
# Define the function of sending SMS
def send_message(sms_code,mobile,expire=5):
""" :param sms_code: The verification code to be sent :param mobile: Mobile number sent :param expire: Expiration time :return: """
# Instantiation sdk object
sdk = SmsSDK(accId = accId,accToken=accToken,appId=appId)
# Prepare the data
tid="1"
datas = ("%s"%sms_code,"%s"%expire) # Tuples
# Send a text message
res = sdk.sendMessage(tid=tid,mobile=mobile,datas=datas)
# analysis json character string
data = json.loads(res)
print(" Data responded by Ronglian cloud :",data)
if data.get('statusCode') == '000000':
return True
return False
2.3 Generate 、 Verify SMS verification code
In the view
# SMS verification code view
import redis
from .utils import send_message
from .tasks import send_message
class SmsCodeAPIView(APIView):
# Send SMS verification code
def get(self,request):
# 1. Get the front-end mobile number
mobile = request.query_params.get("mobile")
# Generate random code
sms_code = str(random.randint(10000,99999))
result = send_message(sms_code,mobile)
# Send verification code synchronously
result = send_message(sms_code,mobile)
if result:
# Store SMS verification code to redis among
r =redis.Redis(host='127.0.0.1',port=6379,password='123123')
key = "sms_%s"%mobile
r.set(key,sms_code,ex=300)
return Response({
"code":200,"msg":' SMS verification code sent successfully ',"sms":sms_code})
else:
return Response({
"code":400,"msg":' fail in send '})
# Verify SMS verification code
def post(self,request):
# Get the front-end mobile number 、 Verification Code
mobile = request.data.get("mobile")
sms_code = request.data.get("sms_code")
# Get the stored verification code
r = redis.Redis(host='127.0.0.1',port=6379,password='123123')
key = "sms_%s"%mobile
stored_code = r.get(key).decode()
if not stored_code:
return Response({
"code":400,
"msg":' The verification code is out of date '
})
if sms_code == str(stored_code):
return Response({
"code":200,
"msg":' Verify success '
})
return Response({
"code":400,
"msg":' Validation failed '
})
边栏推荐
猜你喜欢

Spark常见面试问题整理

With only 5000 lines of code, AI renders 100 million landscape paintings on v853

Data Lake: introduction to Apache iceberg

pyspark学习笔记

Master slave synchronization step read / write separation + self encountered error sharing

Pycharm occupies C disk

Spark common interview questions sorting

Basic concepts of software testing

MySQL statement queries all child nodes of a level node

systemctl-service服务添加环境变量及模板
随机推荐
pyspark学习笔记
Spark common interview questions sorting
Data Lake: viewing data lake from data warehouse
Markdown常用语法记录
Summary of common commands of vim
IMG tag settings height and width are invalid
Pycharm occupies C disk
【无标题】
使用聚类分析 构建信用卡高风险客户识别模型
【无标题】
Five methods to prevent over fitting of neural network
Scattered notes of machine learning: some concepts and notes
MySql语句查询某一级节点的所有子节点
人脸识别神经网络实现
Data Lake: introduction to Apache iceberg
Machine learning algorithm for large factory interview (6) time series analysis
JDBC数据库连接池
JWT header进行编码过程
通用视图,序列化器
Hyperlink de underlined code