def send_sms_captcha(request): telephone = request.GET.get('telephone') # 获取从浏览器传来的手机号码 print(telephone) if User.objects.filter(telephone=telephone).exists(): return JsonResponse({"code": 302, "message": "手机号码已注册"}) text = cache.get(telephone) if not text: code = Captcha.gene_text() # 生成4位短信验证码 result = send_sms(telephone, code=code) # 发送验证码并返回发送结果 result_status = json.loads(result)["Code"] # 获取短信验证码发送状态 print(result_status) # result_status = "OK" # request.session['sms_captcha'] = code # 设置session 会话中的短信验证码 else: code = text result_status = "repeat" if result_status == "OK": # 发送成功 print("短信验证码为:", code) cache.set(telephone, code, 300) result = "OK" elif result_status == "repeat": # 验证码 n 秒 内有效 result = "repeat" print("验证码 n 秒 内有效哦!") elif result_status == "isv.MOBILE_NUMBER_ILLEGAL": result = 'telephone error' else: print("验证码发送败,错误码:", result_status) result = 404 return JsonResponse({"code": result})
def sms_captcha(request): code = Captcha.gene_text() # /accoutn/sms_captcha/?telephone=12345678900 telephone = request.GET.get('telephone') request.session['sms_captcha'] = code result = aliyun.send_sms(telephone, code=code) print('短信验证码:%s' % code) return HttpResponse('success')
def sms_captcha(request): code = Captcha.gene_number() telephone = request.GET.get('telephone') request.session['sms_captcha'] = code result = aliyun.send_sms(telephone,code=code) print(code) return HttpResponse('success')
def sms_captcha(request): """短信验证码""" code = Captcha.gene_text() print('code:', code) # 获取方式是/account/sms_captcha/?telephone=12345678900 telephone = request.GET.get('telephone') result = aliyun.send_sms(telephone, code=code) # 前端会用到该参数 print(telephone, code) request.session['sms_captcha'] = code return HttpResponse('success')
def sms_captcha(request): code = Captcha.gene_text() # /accoutn/sms_captcha/?telephone=12345678900 # 这里的telephone6是从auth.js文件中的data中传过来的两边要保持一致 telephone = request.GET.get('telephone6') #这里设置session是为了把短信验证码存储到session中,然后需要和用户输入的短信验证码做对比用的 request.session['sms_captcha'] = code #这行注释掉后不会真的发送短信到手机上,但可以从后台看到(节约点钱) # result = aliyun.send_sms(telephone,code=code) print('短信验证码:%s' % code) return HttpResponse('success')
def img_captcha(request): text,image = Captcha.gene_code() out = BytesIO() image.save(out,'png') out.seek(0) response = HttpResponse(content_type='image/png') response.write(out.read()) response['Content-length'] = out.tell() request.session['img_captcha'] = text return response
def img_captcha(request): text, image = Captcha.gene_code() out = BytesIO() # 这里需要将文件转换成 数据流 image.save(out, 'png') response = HttpResponse(content_type='image/png') out.seek(0) response.write(out.read()) # out.getvalue() 也可以获取文件数据,但获取文件大小时困难些 response['Content-length'] = out.tell() # tell返回当前读写位置,此时指针已经到末尾 request.session['img_captcha'] = text.lower() # 将验证码存入session会话 print("img_captcha_session = ", text.lower()) return response
def image_captcha(request): text,image = Captcha.gene_code() # image 不是一个Httpresponse可识别的对象 # 因此先要将image变成一个数据流才能放到Httpresponse上 # bytesIO 相当于一个管道, 可以用来存储二进制字节流 print('获取image',text,image) out = BytesIO() image.save(out,'png') out.seek(0) # 设置文件指针 response = HttpResponse(content_type='image/png') response.write(out.read()) response['Content-length'] = out.tell() request.session['img_captcha'] = text return response
def img_captcha(request): text, image = Captcha.gene_code() # image不是一个HttpResponse可以识别的对象 # 因此先要将image变成一个数据流才能放到HttpResponse上 # BytesIO:相当于一个管道,可以用来存储字节流的 out = BytesIO() image.save(out, 'png') # 将文件指针设置到0的位置 out.seek(0) response = HttpResponse(content_type='image/png') response.write(out.read()) response['Content-length'] = out.tell() # 这里设置session是为了把图形验证码存储到session中,然后需要和用户输入的图形验证码做对比用的 request.session['img_captcha'] = text return response
def img_captcha(request): text, image = Captcha.gene_code() out = BytesIO() image.save(out, 'png') out.seek(0) response = HttpResponse(content_type='image/png') response.write(out.read()) response['Content-length'] = out.tell() connection_pool = redis.ConnectionPool(host='119.23.46.9', port='6379') _redis = redis.Redis(connection_pool=connection_pool) _redis.set('img_captcha', text, 600) return response
def img_captcha(request): """图形验证码""" text, image = Captcha.gene_code() # image不是一个HttpResponse可识别对象 # 因此先要将image变成一个数据流才能放到HttpResponse上 # ByteIO:相当于一个管道,用来存储二进制字节流的 out = BytesIO() image.save(out, 'png') # 文件指针设置为0的位置,按照存储,从0开始 out.seek(0) response = HttpResponse(content_type='image/png') response.write(out.read()) response['Content-length'] = out.tell() request.session['img_captcha'] = text return response
def send_emails(request): # send_mail的参数分别是 邮件标题,邮件内容,发件箱(settings.py中设置过的那个), code = Captcha.gene_text().lower() # 生成4位验证码 email = request.GET.get('email') print('email = ', email) if User.objects.filter(email=email).exists(): return JsonResponse({"message": "该邮箱已被绑定"}) email_title = "【盘根学院邮箱激活码】" email_body = "盘根学院邮箱激活码:%s ,激活码5分钟内有效!" % code email_cache = cache.get(email) print('email_cache = ', email_cache) if not email_cache: send_status = send_mail(email_title, email_body, DEFAULT_FROM_EMAIL, [email]) cache.set(email, code, 300) message = '邮件已发送' else: message = '验证码5分钟内有效哦!' return JsonResponse({'code': code, 'message': message})