Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of sending SMS CAPTCHA Code by Django Asynchronous programming

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "Django asynchronous programming to send SMS CAPTCHA code example", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Django asynchronous programming to send SMS CAPTCHA code example"!

Catalogue

Celery

1.celery introduction

1.1 examples of celery applications

1.2 Celery has the following advantages

1.3 Celery characteristics

two。 working principle

2.1 Celery plays the role of producer and consumer

3. Send text messages asynchronously

Create a celery file under the same level directory of 1.settings

two。 Configure the settings file

3 configure the init file under the same level directory of settings

4. Create a new task.py file under utils

5. Call in the interface

6. First start the django project and then open another terminal cd to the project

Celery1.celery introduces 1.1 examples of celery applications

Celery is a distributed asynchronous message task queue developed based on python, through which you can easily implement asynchronous processing of tasks. If you need asynchronous tasks in your business scenario, you can consider using celery.

It may take a long time for you to execute a batch command on 100 machines, but you don't want your program to wait for the result to return. Instead, you will return a task ID. After a period of time, you only need to hold the task id to get the task execution result. While the task execution ing is in progress, you can continue to do other things.

Celery needs to use a message middleware to receive and send task messages and store task results when performing tasks. Generally, rabbitMQ or Redis is used.

1.2 Celery has the following advantages

Simple: after being familiar with the workflow of celery, it is relatively easy to configure and use it.

High availability: when a task fails or when a connection breaks during execution, celery automatically tries to re-execute the task quickly: a single-process celery can handle millions of tasks per minute

Flexibility: almost every component of celery can be extended and customized

1.3 Celery characteristics

It is convenient to check the execution of scheduled tasks, such as success, current status, time spent executing tasks, etc.

Optional multi-process, Eventlet and Gevent models are executed concurrently.

Celery is language-independent. It provides interface support for common languages such as python.

two。 Working principle 2.1 Celery acts as a producer and consumer

Celery Beat: task Scheduler. The Beat process reads the contents of the configuration file and periodically sends the tasks that need to be executed when it expires in the configuration to the task queue.

Celery Worker: consumers who perform tasks usually run multiple consumers on multiple servers to improve operational efficiency.

Broker: message broker, queue itself. Also known as message middleware. Accept the task messages sent by the task producer, store them in the queue and distribute them sequentially to the task consumers (usually message queues or databases).

Producer: task producer. Call Celery API, function, or decorator, and it is the task producer who generates the task and hands it over to the task queue.

Result Backend: save status information and results for query after task processing.

3. The celery file from_ _ future__ import absolute_import is created under the same directory of asynchronous SMS 1.settings, and unicode_literalsimport osfrom celery import Celery# sets the environment variable os.environ.setdefault ('DJANGO_SETTINGS_MODULE',' meiduo.settings') # APPapp for registering Celery = Celery ('meiduo') # binding profile app.config_from_object (' django.conf:settings') Namespace='CELERY') # automatically discovers tasks.py files under each app app.autodiscover_tasks () 2. Configure the settings file CELERY_BROKER_URL = 'redis://127.0.0.1:6379/'CELERY_RESULT_BACKEND =' redis://127.0.0.1:6379/'CELERY_RESULT_SERIALIZER = 'json'3 configure the init file from _ _ future__ import absolute_import, unicode_literalsfrom .celery import app as celery_app__all__ = [' celery_app'] 4. Create a new task.py file under utils from celery.task import taskfrom. Comm import send_message# defines the method to send mail @ taskdef mail (mobile,code): send_message (mobile,code,5) 5. Call from utils.tasks import mailimport randomclass SendMes (APIView) in the API: # SMS verification def get (self) Request): # receiving data sent by the client imagecode = request.query_params.get ('imagecode') print (imagecode) mobile = request.query_params.get (' mobile') print (mobile) uuid = request.query_params.get ('uuid') print (uuid) if not all ([imagecode) Mobile]): return Response ({'msg':' did not get'}) # verify the picture verification code conn = get_redis_conn () # redis take the verification code code = conn.get (uuid) print (code) if code: code = str (code Encoding='utf8') # Picture verification code comparison if imagecode.lower () = = code.lower (): # call the SMS API sms_code = random.randint (10000999999) # key points after verification is passed! Result = mail.delay (mobile,sms_code,1) # join SMS message sent successfully if result: # do you want to save SMS message verification in redis conn.setex (mobile,60) Sms_code) # remove the image verification code from redis conn.delete (uuid) return Response ({'msg':sms_code}) else: return ({' msg':' failed'}) else: return Response ( {'msg':' verification code is incorrect'}) return Response ('ok') 6. First start the django project and then open another terminal cd to the project

The specified number of concurrency of starting celery service under the directory-- autoscale (maximum, minimum)

Celery worker-A meiduo-- loglevel=info-- pool=solo-- autoscale=50,5 Thank you for your reading. The above is the content of "Django asynchronous programming to send SMS CAPTCHA code example". After the study of this article, I believe you have a deeper understanding of the problem of Django asynchronous programming to send SMS CAPTCHA code examples, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report