当前位置:网站首页>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
边栏推荐
- Armeabi-v7a architecture (sv7a)
- About 1931cie -- conversion of XYZ color coordinate graph to RGB color coordinate relationship
- [development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine
- C语言括号匹配(栈括号匹配c语言)
- PTA (daily question) 7-73 turning triangle
- Outlier detection and open set identification (1)
- Jupyter notebook中5个有趣的魔法命令
- 【网络安全】通过iptables和ipset完成服务器防火墙黑名单和白名单功能
- 分布式限流 redission RRateLimiter 的使用及原理
- MySQL的隔离级别、可能出现的问题(脏读、不可重复读、幻读)及其解决方法
猜你喜欢
[development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine
NPM run serve stuck at 40%
Locally connect to redis on Windows Server
MQ 消息丢失、重复、积压问题,如何解决?
数仓搭建——ADS层
"Food alliance ordering system"
IMG tags prohibit dragging pictures
Dynamic programming problem (VIII)
2022dasctfjuly empowerment competition (reappearance)
Dynamic programming (V)
随机推荐
Idea connection database
主线程与守护线程
Common sparse basis and matlab code for compressed sensing
15. Model evaluation and selection
【网络安全】通过iptables和ipset完成服务器防火墙黑名单和白名单功能
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
Recursion / backtracking (middle)
Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)
NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
CUDA相关
I don't know how lucky the boy who randomly typed the log is. There must be a lot of overtime!
Table custom style row class name in elemenui
Dynamic programming problem (6)
Data warehouse construction - ads floor
mysql时间按小时格式化_mysql时间格式化,按时间段查询的MySQL语句[通俗易懂]
Flask sends verification code in combination with Ronglian cloud
Rk3399 9.0 driver add powser button
“吃货联盟定餐系统”
1331. 数组序号转换 : 简单模拟题
Application and principle of distributed current limiting redistribution rratelimiter