1.使用celery异步发送短信

1.1 在 celery_task/mian.py 中添加发送短信函数

# celery项目中的所有导包地址, 都是以CELERY_BASE_DIR为基准设定.
# 执行celery命令时, 也需要进入CELERY_BASE_DIR目录执行.
CELERY_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@app.task(bind=True)
def send_sms_code(self, mobile, datas):
sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../syl'))
# 在方法中导包
from utils.rl_sms import send_message
# time.sleep(5)
try:
# 用 res 接收发送结果, 成功是:0, 失败是:-1
res = send_message(mobile, datas)
except Exception as e:
res = '-1'
if res == '-1':
# 如果发送结果是 -1 就重试.
self.retry(countdown=5, max_retries=3, exc=Exception('短信发送失败'))

1.2 在 verifications/views.py 中添加celery发送短信试图函数

class SmsCodeView(APIView):
"""使用apiview的限流"""
# 1. 所有人可以访问
permission_classes = (AllowAny,)
def post(self, request):
# 1. 获取参数
phone = request.data.get('phone') # 手机号
image_code = request.data.get('image_code') # 图片验证码
image_code_uuid = request.data.get('image_code_uuid') # 前端生成的uuid
# 2. 检查参数
if not all([phone, image_code, image_code_uuid]):
return Response({"code": 999, "msg": "参数不全"})
if not re.match(r'^1[3456789]\d{9}$', phone):
return Response({"code": 999, "msg": "手机号码不正确"})
# 3. 检查是否发送
redis_client = get_redis_connection('img_code')
phone_exists = redis_client.get(phone)
if phone_exists:
return Response({"code": 999, "msg": "频繁发送, 请稍后再试"})
# 4.检查验证图形验证码
redis_image_code = redis_client.get(image_code_uuid) # bytes
if redis_image_code:
# bytes 转成 string
redis_image_code = redis_image_code.decode()
# 比较用户提供的图片内容是否和redis中保存的一致
if image_code.upper() != redis_image_code:
return Response({'code': 999, 'msg': '图片验证码不正确'})
# 5. 发送
code = '%06d' % random.randint(0, 999999) # 随机6位验证码
from syl.settings import BASE_DIR
sys.path.insert(0, os.path.join(BASE_DIR, '../celery_task'))
from main import send_sms_code # 必须这么写, 从main中导包
send_sms_code.delay(phone, (code, "5"))
print(code)
# 6.使用 pipeline 批量操作
pl = redis_client.pipeline() # 实例化pipeline对象
pl.setex(phone, 60 * 5, code) # 存储phone:code, 5分钟有效期
pl.delete(image_code_uuid) # 从redis中删除这个图片验证码, 以防再次被使用
pl.execute()
# 7. 返回结果
return Response({"code": 0, "msg": "短信发送成功"})

1.3添加路由

urlpatterns = [
path('sms_codes/', views.SmsCodeView.as_view()),
]

celery异步发送短信的更多相关文章

  1. 使用celery异步发送短信

    目录 1.使用celery异步发送短信 1.1在celery_task/mian.py中添加发送短信函数 1.2在verifcations/views.py中添加celery发送短信视图函数 1.3 ...

  2. day8(使用celery异步发送短信)

    1.1在celery_task/mian.py中添加发送短信函数 # celery项目中的所有导包地址, 都是以CELERY_BASE_DIR为基准设定. # 执行celery命令时, 也需要进入CE ...

  3. celery 异步发送短信验证码、延迟任务

    短信 celery.py import os, django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "luffy ...

  4. JAVA实现多线程处理批量发送短信、APP推送

    /** * 推送消息 APP.短信 * @param message * @throws Exception */ public void sendMsg(Message message) throw ...

  5. 移动MAS短信平台发送短信

    MAS短信平台发送短信分为两种方式 参考文档下载 一.sdk调用 using mas.ecloud.sdkclient; using System; namespace 短信发送 { class Pr ...

  6. PHP发送短信功能

    发送短信的功能主要在于获得短信接口后,在函数中模仿用户行为,例如浏览器跳转输出短信接口的链接. 需要运用的函数为 curl_init(); curl_setopt(); curl_exec(); cu ...

  7. WPF MVVM下做发送短信小按钮

    最近做一个项目,因为涉及到注册,因此需要发送短信,一般发送短信都有一个倒计时的小按钮,因此,就做了一个,在此做个记录. 一.发送消息 没有调用公司的短信平台,只是模拟前台生成一串数字,将此串数字输出一 ...

  8. NetCore 阿里大于发送短信

    使用阿里大于API发送短信,但阿里没有提供NetCore 的API,自己看了下源码重写了发短信这个部分 public class MessageSender { private readonly st ...

  9. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

随机推荐

  1. 【Kata Daily 191012】Find numbers which are divisible by given number

    题目: Complete the function which takes two arguments and returns all numbers which are divisible by t ...

  2. JavaScript之构造函数

    在学习构造函数之前我们需要知道我们学习构造函数需要学习什么: 1.什么是构造函数  2.构造函数用来做什么 3.构造函数的执行过程  4.构造函数的返回值 1.所以首先我们需要知道什么是构造函数: 在 ...

  3. tensorflow-gpu2.1.0报错 so returning NUMA node zero解决办法

    >>> print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))2020-06-06 10:14:08.92 ...

  4. 今天的一个SQL题-case语句和decode函数

    数据库表: select * from rec order by rst,game_time; ID GAME_TIME      RST ------ -------------- ---- 2 0 ...

  5. 基于FFmpeg的Dxva2硬解码及Direct3D显示(五)

    解码及显示 目录 解码及显示 解码 显示 资源清理 解码 循环读取视频帧 AVPacket packet = { 0 }; while (av_read_frame(m_pFmtCtx, &p ...

  6. 记最近一次ceph故障修复

    前言 所谓吃一堑长一智,每次面对问题才是最好的学习机会,在面对问题的时候,尽量是能够自己去解决,或者去尝试能够最接近答案,确实无法解决再去寻求他人帮助,这样成长的会更快一些,在学校读书做题的时候,老师 ...

  7. 卸载python安装的软件

    python源码安装的软件是无法通过命令卸载的,这个可以通过记录安装过程的形式来卸载安装的软件 以 python2.7.2 为例,在这个目录中有一个 setup.py 的文件,很显然这是安装程序,还是 ...

  8. 理解 ASP.NET Core: 验证

    ASP.NET Core 验证 通常在应用程序中,安全分为前后两个步骤:验证和授权.验证负责检查当前请求者的身份,而授权则根据上一步得到的身份决定当前请求者是否能够访问期望的资源. 既然安全从验证开始 ...

  9. js常用通用函数

    //判断是否数字 function isNumber(str){ var n = Number(str); if (!isNaN(n)){ return true; }else{ return fal ...

  10. jboss反序列化漏洞实战渗透笔记

    一.利用shodan,fofa或谷歌搜索关键字:8080/jmx-console/ 二.下载java反序列化终极测试工具进行验证漏洞 三.记住User Current Directory:  C:\j ...