当前位置:网站首页>Send SMS verification code asynchronously using Ronglian cloud celery
Send SMS verification code asynchronously using Ronglian cloud celery
2022-07-29 00:47:00 【:D...】
List of articles
Celery/ Rong Lianyun Installation and command
celery, Distributed asynchronous task queue
eventlet, Concurrent Network Library gevent Process library
ronglian_sms_sdk The method of Ronglian cloud encapsulation
install
pip install celery==4.4.7
pip install eventlet==0.26.1
pip install ronglian_sms_sdk
celery Start command
celery -A Project name worker -l info -P eventlet
One 、Celery
1. To configure Celery
Create a folder with the same name as the project celery.py file
# Import celery Import settings The configuration file
from celery import Celery
import os
from django.conf import settings
# Configure environment variables celery Of worker Process is disengagement django start-up , But rely on its configuration file
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'p6_421.settings')
# establish celery application app Worker Worker , The person who performs the task myworker
app = Celery('myworker')
# To configure app
app.conf.broker_url = 'redis://:@127.0.0.1:6379/5' # It is equivalent to the queue in the data structure , Broker Put the task in the queue temporarily , wait for Worker To deal with it .
app.conf.result_backend = 'redis://:@127.0.0.1:6379/6' # Backend Used to save the execution result of the task
# Find the task function autodiscover_tasks Auto discovery tasks
app.autodiscover_tasks(settings.INSTALLED_APPS)
2. Create a task file
App users Create under folder tasks.py file Task Mission
The file stores tasks to be processed asynchronously
Two 、 Rong Lianyun
1. Configure Ronglian cloud settings.py
accId = ‘ The primary account assigned by Ronglian cloud communications ID’
accToken = ‘ The primary account assigned by Ronglian cloud communications TOKEN’
appId = ‘ Application of Ronglian cloud communication distribution ID’
settings.py File configuration
ACCID = '8a216da8804ba8a50180x64a39933069b'
ACCTOKEN = '09880a5e3fc94x046b3cc147e240ad169'
APPID = '8a216dax8804ba8a5018064a39a3706a2'
2. Use SmsSDK Send a text message
stay App users Under folder tasks.py In the document
tasks.py file
from ronglian_sms_sdk import SmsSDK
from p6_421 import settings
from p6_421.celery import app
# With the help of SMS verification code
@app.task
def send_message(sms_code, mobile, expire=5): # expire=5 5 Minutes expired
# Instantiation sdk
sdk = SmsSDK(settings.ACCID, settings.ACCTOKEN, settings.APPID)
# Organization data
tid = '1' # The SMS template of ronglianyun ID
mobile = "%s" % mobile
datas = ("%s" % sms_code, "%s" % expire) # first : SMS verification code the second : How long overdue
# Send a text message And receive the response
res = sdk.sendMessage(tid=tid, mobile=mobile, datas=datas)
return res
3、 ... and 、 View authoring
1. Send SMS verification code
# Generate SMS verification code Please let Lianyun Send a text message asynchronous
class SmsView(APIView):
def post(self, request):
mobile = request.data.get('mobile')
uuid_ = request.data.get('uuid')
# 2 Generate SMS verification code and redis Storage Easy to verify
sms_code = '%d' % random.randint(10000, 99999)
redis_cli = redis.Redis()
redis_cli.set(uuid_, sms_code, ex=300)
# 3 Please let Lianyun Send a text message .delay Perform tasks asynchronously
res = send_message.delay(sms_code, mobile)
return Response({
'code': 200, 'msg': ' SMS sent , Pay attention to check !'})
2. Verify SMS verification code ( According to the front-end requirements )
# Verify SMS verification code
class CheckSmsView(APIView):
def post(self, request):
sms_code = request.data.get('sms_code')
uuid_ = request.data.get('code_id')
# Inquire about redis Take out the stored SMS verification code stored_sms_code
redis_cli = redis.Redis()
stored_sms_code = redis_cli.get(uuid_)
# Judge There is & After decoding = Front end captcha
if stored_sms_code and stored_sms_code.decode() == sms_code:
return Response({
'code': 200, 'msg': ' Compare success '})
else:
return Response({
'code': 400, 'error': ' Comparison failed '})
3. Start... First django project Then open another terminal to start celery service
celery -A Project name worker -l info -P eventlet
边栏推荐
- Rk3399 9.0 driver add powser button
- [micro services ~nacos] Nacos service providers and service consumers
- NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- Isolation level of MySQL, possible problems (dirty reading, unrepeatable reading, phantom reading) and their solutions
- 数仓搭建——ADS层
- Summary: the difference between pod and container
- redis版本怎么查看(查看redis进程)
- PTA (daily question) 7-69 narcissus number
- Flyway's quick start tutorial
猜你喜欢

Statistical analysis of time series

Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency

Tips for API interface optimization
![[development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine](/img/a1/9a69e5d123a8a11504da251bd1bcfc.png)
[development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine

芯驰科技发布G9系列最新旗舰产品,配备6个1.8Ghz主频的A55核心

Recursion / backtracking (Part 2)

Cause analysis of 12 MySQL slow queries

Dynamic programming (V)

Shell编程规范与变量

110 MySQL interview questions and answers (continuously updated)
随机推荐
16.偏差、方差、正则化、学习曲线对模型的影响
Dynamic programming problem (VII)
【MySQL 8】Generated Invisible Primary Keys(GIPK)
Talk about seven ways to realize asynchronous programming
乱打日志的男孩运气怎么样我不知道,加班肯定很多!
Outlier detection and open set identification (1)
Brief introduction to compressed sensing
15.模型评估和选择问题
MySQL transaction (this is enough...)
Use hutool tool class to operate excel with more empty Sheet1
Mock.js essay
I don't recommend you use Select*
PTA (daily question) 7-74 yesterday
面试被问到了String相关的几道题,你能答上来吗?
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
保护性拷贝&无状态
会议OA项目之会议通知&会议反馈&反馈详情功能
Common sparse basis and matlab code for compressed sensing
数仓搭建——DWT层
About 1931cie -- conversion of XYZ color coordinate graph to RGB color coordinate relationship