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()),
]

最新文章

  1. VS2015 Update2中有关cordova和xamarin安装的问题
  2. Spring MVC初次相见
  3. Java基础-输入输出-3.编写BinIoDemo.java的Java应用程序,程序完成的功能是:完成1.doc文件的复制,复制以后的文件的名称为自己的学号姓名.doc。
  4. 关于AngularJs中的路由学习总结
  5. mini2440的SDRAM分析
  6. android ant 自动编译打包
  7. android volley get请求使用
  8. IE6不支持li:hover的解决办法,一句代码让IE6支持li:hover
  9. Hadoop2.6.0错误
  10. mysql导入数据load data infile用法
  11. docker的基本使用
  12. 使用window.postMessage实现跨域通信
  13. C#获取上个月的第一天零点和最后一天23点59分59秒
  14. java反射机制性能优化
  15. caffe层解读系列-softmax_loss
  16. juniper防护墙接口的NAT和ROUTE模式如何选择问题
  17. Chrome opacity非1时border-radius圆角边框剪裁问题
  18. 小程序 第一个学习示例(TodoList)
  19. dubbo服务暴露
  20. noode inquirer

热门文章

  1. [C#] (原创)一步一步教你自定义控件——04,ProgressBar(进度条)
  2. 使用Python虚拟环境
  3. -bash: bash_profile: command not found问题
  4. CC模型加载太慢?一招破解!
  5. php判断手机浏览器和pc浏览器
  6. Ceph Bluestore首测
  7. 使用iptables做端口转发
  8. Caused by: java.lang.ClassNotFoundException: com.alibaba.druid.filter.logging.Log4j2Filter
  9. SpringBoot WebSocket 消息交互
  10. ctf-工具-binwalk