标签:ima miss 实例化 turn login 频繁 测试 比较 user
‘‘‘1. 安装容联云sdk‘‘‘ pip install ronglian_sms_sdk # 免费测试文档地址: https://doc.yuntongxun.com/p/5a531a353b8496dd00dcdfe2 # utils/rl_sms.py from ronglian_sms_sdk import SmsSDK accId = ‘8a216da874af5fff01750210423e1a85‘ accToken = ‘2a0eb1887a434dc5a7c5bf5fee4afc35‘ appId = ‘8a216da874af5fff0175021043011a8c‘ def send_message(phone,datas): sdk = SmsSDK(accId, accToken, appId) tid = ‘1‘ resp = sdk.sendMessage(tid, phone, datas) return resp
urlpatterns = [ path(‘sms_codes/‘, views.SmsCodeView.as_view()), ]
from rest_framework.permissions import AllowAny from rest_framework.views import APIView from rest_framework.response import Response import re import random from utils.rl_sms import send_message 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": "频繁发送, 请稍后再试"}) # 验证图形验证码 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")) 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": "短信发送成功"})
标签:ima miss 实例化 turn login 频繁 测试 比较 user
原文地址:https://www.cnblogs.com/spbyyy/p/13778658.html