def test_gen_mobile_code(self): pattern = re.compile(r'[0-9]{6}') for _ in range(1000): self.assertIsNotNone(pattern.match(gen_mobile_code())) pattern = re.compile(r'\d{4}') for _ in range(1000): self.assertIsNotNone(pattern.match(gen_mobile_code(length=4)))
def get_code_by_sms(request, tel): """获取短信验证码""" if check_tel(tel): if caches['default'].get(tel): resp = DefaultResponse(*CODE_TOO_FREQUENCY) else: code = gen_mobile_code() message = f'您的短信验证码是{code},打死也不能告诉别人哟!【Python小课】' # 通过异步化函数的delay方法让函数异步化的执行,这个地方就相当于是消息的生产者 # 如果要完成这个任务还需要消息的消费者,需要其他的进程来处理掉这条消息 # 消费者跟生产者可以是不同的机器(通常情况下也是如此) # celery -A zufang worker -l debug # send_sms_by_luosimao.delay(tel, message) # task = send_sms_by_luosimao.s(countdown=10, expires=60) # task.delay(tel, message) send_sms_by_luosimao.apply_async( (tel, message), # {'tel': tel, 'message': message}, queue='queue1', countdown=10, # retry_policy={}, # expires=60, # compression='zlib', ) caches['default'].set(tel, code, timeout=120) resp = DefaultResponse(*MOBILE_CODE_SUCCESS) else: resp = DefaultResponse(*INVALID_TEL_NUM) return resp
def get_code_by_sms(request, tel): """获取短信验证码""" if check_tel(tel): if caches['default'].get(f'{tel}:block'): resp = DefaultResponse(*CODE_TOO_FREQUENCY) else: code = gen_mobile_code() message = f'您的短信验证码是{code},【软件开发】' # 使用delay()方式去执行且参数是没有变化 send_sms_by_luosimao.delay(tel, message=message) caches['default'].set(f'{tel}:block', code, timeout=120) # 阻止一定时间内禁止重发 caches['default'].set(f'{tel}:valid', code, timeout=1800) # 设置验证码的有效时间段 resp = DefaultResponse(*MOBILE_CODE_SUCCESS) else: resp = DefaultResponse(*INVALID_TEL_NUM) return resp
def get_code_by_sms(request, tel): """获取短信验证码""" if check_tel(tel): if caches['default'].get(f'{tel}:block'): resp = DefaultResponse(*CODE_TOO_FREQUENCY) else: code = gen_mobile_code() message = f'您的短信验证码是{code},打死也不能告诉别人哟!【Python小课】' send_sms_by_luosimao.apply_async((tel, message), countdown=random.random() * 5) caches['default'].set(f'{tel}:block', code, timeout=120) caches['default'].set(f'{tel}:valid', code, timeout=1800) resp = DefaultResponse(*MOBILE_CODE_SUCCESS) else: resp = DefaultResponse(*INVALID_TEL_NUM) return resp
def get_code_by_sms(request, tel): if check_tel(tel): if caches['default'].get(f'{tel}:block'): res = DefaultResponse(*CODE_TOO_FREQUENCY) else: code = gen_mobile_code() time.sleep(random.random() * 10) message = f'您的短信验证码为{code}, 如非本人操作请忽略【铁壳网络】' print(message) # 成为消息的生产者,并将任务加入队列 # send_sms_by_luosimao.apply_async((message, tel), countdown=random.random() * 10) caches['default'].set(f'{tel}:block', code, timeout=120) caches['default'].set(f'{tel}:valid', code, timeout=1800) res = DefaultResponse(*MOBILE_CODE_SUCCESS) else: res = DefaultResponse(*INVALID_TEL_NUM) return res
def register(request): """用户注册""" if request.method == 'GET': return render(request, 'register.html') else: try: if request.POST['code'] == request.session['code']: form = RegisterForm(request.POST) if form.is_valid(): del form.cleaned_data['repassword'] del form.cleaned_data['code'] form.cleaned_data['token'] = uuid.uuid1().hex form.cleaned_data['lastvisit'] = timezone.now() user = User(**form.cleaned_data) user.save(is_insert=True) request.session['code'] = gen_mobile_code() return render(request, 'login.html', {'hint': '注册成功请登录'}) else: hint = handle_register_errors(form.errors) else: hint = '请输入正确的手机验证码' except KeyError: hint = '请先获取手机验证码再完成注册' return render(request, 'register.html', {'hint': hint})
def test_gen_mobile_code(self): pattern = re.compile(r'\d{6}') for _ in range(10000): self.assertRegex(gen_mobile_code(), pattern)