Example #1
0
def sms(mobile):
    """发送短信验证码"""
    # 验证手机
    if not re.match("^1[3-9]\d{9}$", mobile):
        return {"errno": status.CODE_VALIDATE_ERROR, "errmsg": message.mobile_format_error}

    # 短信发送冷却时间
    ret = redis.get("int_%s" % mobile)
    if ret is not None:
        return {"errno": status.CODE_INTERVAL_TIME, "errmsg": message.sms_interval_time}

    # 生成验证码
    sms_code = "%06d" % random.randint(0, 999999)
    print(">>>>>>>>>>>>>>>>", sms_code)
    try:
        print(">>>>>>>>>>>>>>>>111>>>>>>>>>>>>>>>>")
        from mycelery.sms.tasks import send_sms
        # 异步发送短信
        send_sms.delay(mobile=mobile, sms_code=sms_code)
        print(">>>>>>>>>>>>>>>>222>>>>>>>>>>>>>>>>")
        # 返回结果
        return {"errno": status.CODE_OK, "errmsg": message.sms_is_send}
    except Exception as e:
        print(">>>>>>>>>>>>>>>>333>>>>>>>>>>>>>>>>")
        return {"errno": status.CODE_SMS_ERROR, "errmsg": message.sms_send_error}
 # todo
#  celery or websocket bug
Example #2
0
    def get(self, request, mobile):
        #接受手机号码,验证是否已注册
        log.error(mobile)
        try:
            User.objects.get(mobile=mobile)
            return Response('当前手机号码已被注册', status=status.HTTP_400_BAD_REQUEST)
            log.error('手机号码已注册%s' % mobile)
        except:
            pass

        #生成短信号码并把短信和手机号保存到redis中
        #获取随机的6位数
        code = "%06d" % random.randint(0, 99999)
        #获取验证码过期时间
        expire_time = SMS_EXPIRE_TIME // 60
        #短信模板id
        expire_id = SMS_ID

        try:
            # 引入redis

            redis = get_redis_connection("sms_code")
            mobile_interver = redis.get("mobile_%s" % mobile)
            if mobile_interver:
                return Response({'msg': '请稍后再试'},
                                status=status.HTTP_400_BAD_REQUEST)
                log.error('手机验证码未过期%s' % mobile)
            #在以后开发中如果要一次性写入多条redis命令,建议使用事务操作
            pip = redis.pipeline()
            pip.multi()
            # 设置间隔时间
            pip.setex("mobile_%s" % mobile, SMS_INTERVAL_TIME,
                      "_")  #"-"表示占位符,没有任何意义

            pip.setex('sms_%s' % mobile, expire_time, code)

            #执行管道中所有命令
            pip.execute()

            #改成调用一步任务队列celery
            # print(mobile, code)
            # print('---------------')
            send_sms.delay(mobile, code)

            #发送短信
            # 参数说明,接受短信的手机号,【短信验证码,短信有效期】,短信模板id
            # 模板id在测试阶段是1
            # ccp = CCP()
            # result = ccp.send_template_sms(mobile, [code, expire_time], expire_id)
            # if result == -1:
            #     log.error('发送短信失败!手机号:%s'%mobile)
            #     return Response({'msg':'短信发送失败'},status = status.HTTP_500_INTERNAL_SERVER_ERROR)
        except:
            log.error('发送短信失败!')
            return Response({'msg': '服务器异常'},
                            status=status.HTTP_507_INSUFFICIENT_STORAGE)

        return Response({'msg': '短信发送成功'}, status=status.HTTP_200_OK)
Example #3
0
    def get(self, request, mobile):
        # 生成短信验证码
        sms_code = "%06d" % random.randint(0, 999999)

        # 保存短信验证码与发送记录
        redis_conn = get_redis_connection("sms_code")
        # 使用redis提供的管道操作可以一次执行多条redis命令
        pl = redis_conn.pipeline()
        pl.multi()
        pl.setex('sms_%s' % mobile, 300, sms_code)
        pl.setex('sms_time_%s' % mobile, 60, 1)
        pl.execute()

        # 发送短信验证码
        ccp = CCP()
        # ccp.send_template_sms(mobile, [sms_code, "60"], 1)
        send_sms.delay(mobile, sms_code, constants.SMS_INTERVAL_TIME)

        return Response({"message": "OK"}, status.HTTP_200_OK)
Example #4
0
def test(request):
    # 异步任务

    # 1. 声明一个和celery一模一样的任务函数,但是我们可以导包来解决

    send_sms.delay("110")
    send_sms2.delay("119")
    # send_sms.delay() 如果调用的任务函数没有参数,则不需要填写任何内容

    # 定时任务

    # ctime = datetime.now()
    # # 默认用utc时间
    # utc_ctime = datetime.utcfromtimestamp(ctime.timestamp())
    # time_delay = timedelta(seconds=10)
    # task_time = utc_ctime + time_delay
    # result = send_sms.apply_async(["911", ], eta=task_time)
    # print(result.id)

    return HttpResponse('ok')
Example #5
0
    def get(self, request, mobile):
        """短信发送"""

        # 1. 判断手机号码是否在60秒内曾经发送过短信
        redis_conn = get_redis_connection("sms_code")
        ret = redis_conn.get("mobile_%s" % mobile)
        if ret is not None:
            return Response({"message": "对不起,60秒内已经发送过短信,请耐心等待"},
                            status=status.HTTP_400_BAD_REQUEST)

        # 2. 生成短信验证码
        sms_code = "%06d" % random.randint(1, 999999)

        # 3. 保存短信验证码到redis[使用事务把多条命令集中发送给redis]
        # 创建管道对象
        pipe = redis_conn.pipeline()
        # 开启事务【无法管理数据库的读取数据操作】
        pipe.multi()
        # 把事务中要完成的所有操作,写入到管道中
        pipe.setex("sms_%s" % mobile, constants.SMS_EXPIRE_TIME, sms_code)
        pipe.setex("mobile_%s" % mobile, constants.SMS_INTERVAL_TIME, "_")
        # 执行事务
        pipe.execute()

        # 4. 调用短信sdk,发送短信
        try:
            from mycelery.sms.tasks import send_sms
            send_sms.delay(mobile, sms_code)

            # ccp = CCP()
            # ret = ccp.send_template_sms(mobile, [sms_code, constants.SMS_EXPIRE_TIME//60], constants.SMS_TEMPLATE_ID)
            # if not ret:
            #     log.error("用户注册短信发送失败!手机号:%s" % mobile)
            #     return Response({"message":"发送短信失败!"},status=status.HTTP_500_INTERNAL_SERVER_ERROR)
        except:
            return Response({"message": "发送短信失败!"},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # 5. 响应发送短信的结果
        return Response({"message": "发送短信成功!"})
Example #6
0
    def get(self, request, mobile):
        # from mycelery.mail.tasks import send_mail
        # send_mail.delay("*****@*****.**")
        """短信发送"""
        redis_conn = get_redis_connection('sms_code')
        ret = redis_conn.get('mobile_%s' % mobile)
        if ret is not None:
            return Response({"message": '对不起,60秒内已经发送过短信,请耐心等待'},
                            status=http_status.HTTP_400_BAD_REQUEST)

        # 2 .生成短信验证码
        sms_code = '%06d' % random.randint(1, 999999)

        # 3.保存短信验证码到redis
        # 开启事务【无法管理数据库的读取数据操作】
        pipe = redis_conn.pipeline()
        # redis_conn.setex("sms_%s" % mobile, constants.SMS_EXPIRE_TIME, sms_code)
        pipe.setex("sms_%s" % mobile, constants.SMS_EXPIRE_TIME, sms_code)
        # redis_conn.setex("mobile_%s" % mobile, constants.SMS_EXPIRE_TIME, '_')
        pipe.setex("mobile_%s" % mobile, constants.SMS_EXPIRE_TIME, '_')
        pipe.execute()
        # 4. 调用短信sdk, 发送短信
        # try:
        # ccp = CCP()
        print(mobile, sms_code, constants.SMS_EXPIRE_TIME // 60,
              constants.SMS_TEMPLATE_ID)

        # ret = ccp.send_template_sms(mobile, [sms_code, constants.SMS_EXPIRE_TIME // 60], constants.SMS_TEMPLATE_ID)
        # print('ret:>', ret)
        send_sms.delay(mobile, sms_code)
        # ret = ccp.send_template_sms(mobile, [sms_code, constants.SMS_EXPIRE_TIME // 60], constants.SMS_TEMPLATE_ID)
        # if ret == -1:
        #     log.error('用户注册短信发送失败!手机号:%s' % mobile)
        #     print(1)
        #     return Response({'message': '发送短信失败!'},status=http_status.HTTP_500_INTERNAL_SERVER_ERROR)
        # except:
        #     print(2)
        #     return Response({'短信发送失败!'}, status=http_status.HTTP_500_INTERNAL_SERVER_ERROR)

        return Response({"message": "发送短信成功!"})
Example #7
0
    def get(self, request, mobile):
        # 保存短信验证码与发送记录
        redis_conn = get_redis_connection('sms_code')
        ret = redis_conn.get("send_%s" % mobile)
        # 1. 判断是否冷却
        if ret is not None:
            return Response({})
        # 2. 生成短信验证码
        sms_code = "%06d" % random.randint(0, 999999)
        sms_time = constants.SMS_EXPIRE_TIME

        # 3. 发送短信
        send_sms.delay(mobile, sms_code)

        # 3.1 使用管道多次加载, 一次执行多个指令
        pl = redis_conn.pipeline()
        pl.multi()
        pl.setex("sms_%s" % mobile, sms_time, sms_code)  # 设置短信有效期
        pl.setex("sms_time_%s" % mobile, constants.SMS_INTERVAL_TIME,
                 1)  # 设置发送间隔时间
        pl.execute()

        return Response({"message": "短信发送成功!"})
Example #8
0
 def get(self, request, mobile):
     """ 获取短信验证码 """
     # 判断是否处于发送短信冷却时间内
     redis = get_redis_connection("sms_code")
     interval_time = redis.ttl("interval_%s" % mobile)
     if interval_time >= 0:
         return Response({"message": "短信已发送,请%s秒后再点击发送" % interval_time})
     code = "%06d" % random.randint(0, 99999)
     template_code = {'code': code}
     # 记录短信验证码到redis数据库中
     ret = send_sms.delay(mobile, template_code)
     pipe = redis.pipeline(0)  # 创建一个管道对象
     pipe.multi()  # 开启事务
     pipe.setex("sms_%s" % mobile, constants.SMS_EXPIRE_TIME, code)
     pipe.setex("interval_%s" % mobile, constants.SMS_INTERVAL_TIME, "_")
     pipe.execute()  # 执行事物
     return Response({"message": "短信已发送,请留意您的短信"})
Example #9
0
def test(request):
    # 定时任务
    # now_time = datetime.now()
    # utc_now_time = datetime.utcfromtimestamp(now_time.timestamp())
    # time_delay = timedelta(seconds=10)
    # task_time = utc_now_time + time_delay
    # result = send_sms.apply_async(['niu',], eta=task_time)

    # 异步任务
    send_sms.delay('niu')
    send_sms.delay('haibao')
    send_sms.delay('niu')
    send_sms.delay('haibao')
    send_sms.delay('niu')
    send_sms.delay('haibao')
    send_sms.delay('niu')
    send_sms.delay('haibao')
    return HttpResponse(400)