避免频繁发送短信验证码

  1. 提取、校验 send_flag
send_flag = redis_conn.get('send_flag_%s' % mobile)
if send_flag:
    return http.JsonResponse({'code': RETCODE.THROTTLINGERR, 
                              'errmsg': '发送短信过于频繁'})
  1. 重新写入send_flag
# 保存短信验证码
# 短信验证码有效期,单位:秒
# SMS_CODE_REDIS_EXPIRES = 300
redis_conn.setex('sms_code_%s' % mobile, 
                 constants.SMS_CODE_REDIS_EXPIRES, 
                 sms_code)

# 重新写入send_flag
# 60s内是否重复发送的标记
# SEND_SMS_CODE_INTERVAL = 60(s)
redis_conn.setex('send_flag_%s' % mobile, 
                 constants.SEND_SMS_CODE_INTERVAL, 
                 1)
原文地址:https://www.cnblogs.com/oklizz/p/11203570.html