1.celery配置与基本使用
1.1 安装celery
pip install celery==5.0.0
1.2 新建celery/main.py
配置celery
# celery_task/main.py
import os
from celery import Celery
# 定义celery实例, 需要的参数, 1, 实例名, 2, 任务发布位置, 3, 结果保存位置
app = Celery('mycelery',
broker='redis://127.0.0.1:6379/14', # 任务存放的地方
backend='redis://127.0.0.1:6379/15') # 结果存放的地方
@app.task
def add(x, y):
return x + y
2.测试celery
2.1 启动celery
'''1.启动celery'''
#1.1 单进程启动celery
celery -A main worker -l INFO
#1.2 celery管理
celery multi start celery_test -A celery_test -l debug --autoscale=50,5 # celery并发数:最多50个,最少5个
ps auxww|grep "celery worker"|grep -v grep|awk '{print $2}'|xargs kill -9 # 关闭所有celery进程
3.使用celery异步发送短信
3.1 在celery_task/mian.py
中添加发送短信函数
# celery_task/main.py
import os
import sys
import time
from celery import Celery
# 定义celery实例, 需要的参数, 1, 实例名, 2, 任务发布位置, 3, 结果保存位置
app = Celery('mycelery',
broker='redis://127.0.0.1:6379/14', # 任务存放的地方
backend='redis://127.0.0.1:6379/15') # 结果存放的地方
@app.task
def add(x, y):
return x + y
"""
'''1.启动celery'''
#1.1 单进程启动celery
celery -A main worker -l INFO
#1.2 celery管理
celery multi start celery_test -A celery_test -l debug --autoscale=50,5 # celery并发数:最多50个,最少5个
ps auxww|grep "celery worker"|grep -v grep|awk '{print $2}'|xargs kill -9 # 关闭所有celery进程
"""
# celery项目中的所有导包地址, 都是以CELERY_BASE_DIR为基准设定.
# 执行celery命令时, 也需要进入CELERY_BASE_DIR目录执行.
CELERY_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
print(sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../syl')))
try:
@app.task(bind=True)
def send_sms_code(self, mobile, datas):
sys.path.insert(0, os.path.join(CELERY_BASE_DIR, '../syl'))
# 在方法中导包
from libs.rl_sms import send_message
time.sleep(5)
try:
# 用 res 接收发送结果, 成功是:0, 失败是:-1
res = send_message(mobile, datas)
except Exception as e:
print(e)
res = '-1'
if res == '-1':
# 如果发送结果是 -1 就重试.
self.retry(countdown=5, max_retries=3, exc=Exception('短信发送失败'))
except Exception as e:
print(e)
3.2 在verifications/views.py
中添加celery发送短信试图函数
import os
import random
import re
import sys
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse, HttpResponseForbidden
from django.views import View
from django_redis import get_redis_connection
from libs.captcha.captcha import captcha
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from rest_framework.response import Response
#图片验证码接口
class ImageCodeView(View):
def get(self, request):
# 1.接收数据
uuid = request.GET.get('uuid')
# 2.校验数据
if not uuid:
return HttpResponseForbidden('uuid无效')
# 3.处理业务
# 获取图片文本内容和图片二进制代码
text, image = captcha.generate_captcha()
# 4.把uuid和图片文本存入redis
redis_client = get_redis_connection('img_code') # 获取redis客户端
# 5.写入redis(是字符串)
redis_client.setex(uuid, 60 * 5, text)
# 6.返回响应图片
return HttpResponse(image, content_type='image/jpg')
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
print(phone, image_code)
# 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": "频繁发送, 请稍后再试"})
# 验证图形验证码
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': '图片验证码不正确'})
# 4. 发送
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"))
# send_message(phone, (code, "5"))
print(code)
# 5.使用 pipeline 批量操作
pl = redis_client.pipeline() # 实例化pipeline对象
pl.setex(phone, 60 * 5, code) # 存储phone:code, 5分钟有效期
pl.delete(image_code_uuid) # 从redis中删除这个图片验证码, 以防再次被使用
pl.execute()
# 6. 返回结果
return Response({"code": 0, "msg": "短信发送成功"})
3.3 添加路由verifications/urls
from django.urls import path
from . import views
urlpatterns = [
path('image_codes/', views.ImageCodeView.as_view()),
path('sms_codes/', views.SmsCodeView.as_view()),
]
3.4.测试接口
http://192.168.56.100:8888/verify/sms_codes/
请求携带参数
{
"phone": 18538752511,
"image_code":"aed3", # 前端生成的 图形验证码
"image_code_uuid":"de8edce2-fc9f-11ea-9325-005056c00008" # 前端生成的uuid
}
4.0 django添加检查用户名和手机号是否存在接口和注册
4.1在user/urls.py
中添加
urlpatterns = [
path('count/', views.RegCountView.as_view()), # 查询用户名手机号使用量的视图, /user/count/
path('register/', views.RegisterView.as_view()), # 注册视图, /user/register/
]
4.2 在user/views.py
中添加检查视图函数
import datetime
import random
from django.http import HttpResponse
from django.views import View
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.authentication import BasicAuthentication, SessionAuthentication
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.permissions import AllowAny, IsAdminUser, IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from rest_framework.permissions import BasePermission, SAFE_METHODS
from .models import User
from .serializers import UserSerializer, UserUnActiveSerializer, UserInfoSerializer
import redis
redis_client = redis.Redis(db=2)
# 查询用户数量接口
class RegCountView(APIView):
# 注册时需要验证的用户名和手机号是否使用
# 自定义权限类
permission_classes = (AllowAny,)
def post(self, request):
# 接收参数: 验证的内容type: username/phone, data: '用户名' 或者 '手机号',
datatype = request.data.get('type')
data = request.data.get('data')
if not all([data, datatype]):
return Response({'code': 999, 'msg': '参数不完整'})
if datatype == 'username':
count = User.objects.filter(username=data).count()
if datatype == 'phone':
count = User.objects.filter(phone=data).count()
return Response({'code': 0, 'msg': '查询成功', 'data': {'type': datatype, 'count': count}})
4.2 在user/views.py
中添注册查视图函数
# 注册接口
class RegisterView(APIView):
"""
用户注册, 权限是: 匿名用户可访问
"""
# 自定义权限类
permission_classes = (AllowAny,)
def post(self, request):
"""
接收用户名,密码,手机号和验证码, 前端校验两遍一致性, 注册成功后返回成功, 然后用户自行登录获取token
1. 用户名
2. 密码
3. 手机号
4. 验证码
:param request:
:return: {'code':0,'msg':'注册成功'}
code: "260361"
password: "123123"
phone: "13303479527"
username: "liangxuepeng"
"""
username = request.data.get('username')
phone = request.data.get('phone')
code = request.data.get('code')
passwrod = request.data.get('password')
if all([username, passwrod, phone, code]):
print(username, passwrod, phone, code)
else:
return Response({'code': 999, 'msg': '参数不全'})
# rand_name = self.randomUsername()
# 验证手机验证码
# redis_client = get_redis_connection('verify_code')
code_redis = redis_client.get(phone)
if code_redis:
code_redis = code_redis.decode()
if not code == code_redis:
return Response({'code': 999, 'msg': '手机验证码错误'})
user = User(username=username, phone=phone)
user.set_password(passwrod)
user.save()
return Response({'code': 0, 'msg': '注册成功'})
4.3测试检查接口
http://192.168.56.100:8888/user/count/