def get(self, request, uuid): captcha = Captcha() text, image = captcha.generate_captcha() # 连接redis数据库,写入验证码,UUID redis = get_redis_connection('verify') redis.setex('uuid:%s' % uuid, 300, text) return HttpResponse(image, content_type="image/jpg")
def get(self, *args): try: request = args[0] uuid = args[1] email = args[2] except: return HttpResponseBadRequest captcha = Captcha() text = captcha.generate_captcha(flag="text") text = "".join(text) # redis数据库写入 redis = get_redis_connection('email') redis.setex('uuid:%s' % uuid, 300, text) print(text) # 发送邮件 try: send_mail('YesLab教务系统密码重置', '验证码:' + text, settings.EMAIL_HOST_USER, [email], fail_silently=False) stat = 'true' except: stat = 'false' print(stat) return HttpResponse(stat)
def clean_captcha(self): captcha = self.cleaned_data.get('captcha',None) print (captcha) if Captcha.check_captcha(captcha): return self.cleaned_data else: print ('captche is fault') raise forms.ValidationError('captcha errors')
def captcha(request): text, image = Captcha.gene_code() out = StringIO() image.save(out, 'png') out.seek(0) response = HttpResponse(content_type='image/png') response.write(out.read()) return response
def sms_captcha(request): # /sms_captcha/?telephone=xxx telephone = request.GET.get('telephone') code = Captcha.gene_text() cache.set(telephone, code, 5 * 60) print('短信验证码:', code) # result = aliyunsms.send_sms(telephone,code) return restful.ok()
def draw_example(request): text, image = Captcha.gene_code() out = BytesIO() image.save(out, 'png') # 将对象保存到BytesIO中 out.seek(0) # 移动文件指针 cache.set('image_text', text.lower()) httpresponse = HttpResponse(content_type='image/png') httpresponse.write(out.read()) httpresponse["Content-length"] = out.tell() return httpresponse
def graph_captcha(request): text, img = Captcha.gene_code() out = BytesIO() # 塞管道 img.save(out, 'png') out.seek(0) resp = HttpResponse(content_type="image/png") resp.write(out.read()) mcache.set_key(text.lower(), text.lower()) return resp
def sms_captcha(request): # 手机号 telephone = request.GET.get('telephone') # 验证码 captcha = Captcha.gene_text() # 发送短信 # send_sms(telephone, captcha) # 缓存验证码 ret = mcache.set_key(captcha.lower(), captcha.lower()) print('手机号 {} 验证码 {}'.format(telephone, captcha)) # return JsonResponse(str(ret, encoding='utf-8'), safe=False) # return JsonResponse({'code': 0}) return json_status.result(message='验证码发送成功')
def graph_captcha(request): # 获取验证码 text, img = Captcha.gene_code() # BytesIO 二进制 out = BytesIO() # 塞管道 保存文件 保存完成之后 位置在后面 10010100101111 img.save(out, 'png') # 回到最开始位置 游标 11111 out.seek(0) # 返回一个 response JsonResponse(content_type) response resp = HttpResponse(content_type="image/png") # 前面已经塞进去 读出来 resp.write(out.read()) # 验证码存缓存 从前端获取值 和后台比较 如果一致 表示 mcache.set_key(text.lower(), text.lower()) return resp
def sms_captcha(request): # 1. 获取手机号 telephone = request.GET.get('telephone') # print(telephone) # 2. 生成验证码 随机四位验证码 captcha = Captcha.gene_text() # 3. 发送短信 确保短信跑通了一次之后 并不会真正发送 # ret = send_sms(telephone, captcha) # print(ret) b'{"Message":"OK","RequestId":"84DB9F38-C231-4635-8A90-BCF4BFD2AE1F","BizId":"170515639348811432^0","Code":"OK"}' # "{\"Message\":"OK"}" u'在' # 验证码加缓存 单用户注册没有任何问题 set 最后一个为准 这种几率很小 很小 不区分大小写 在缓存里面 存的是小写 # sTsw == stsw sTsw/StSW/stsw 前端获取的时候 也小写 mcache.set_key(captcha.lower(), captcha.lower()) # 返回什么类型 对象/字符串 print('手机号 {} 验证码{}'.format(telephone, captcha)) # return JsonResponse(str(ret, encoding='utf-8'), safe=False) return JsonResponse({"code": 2})
def graph_captcha(request): captcha, image = Captcha.gene_code() # image 是一个函数对象 要变成一个数据流才能被 HttpResponse 识别 # BytesIO 用于存放字节流 # 实例化 out = BytesIO() # 保存图片 image.save(out, 'png') # 设置回到文件最开头 out.seek(0) # 设置类型 response = HttpResponse(content_type="image/png") # 去读文件 并写入response response.write(out.read()) mcache.set_key('graph_captcha', captcha) return response
def get(self): code_id = self.get_argument("codeid") pre_code_id = self.get_argument("pcodeid") if pre_code_id: try: self.redis.delete("image_code_%s" % pre_code_id) except Exception as e: logging.error(e) captcha = Captcha.instance() name, text, image = captcha.generate_captcha() try: self.redis.setex("image_code_%s" % code_id, constants.IMAGE_CODE_EXPIRES_SECONDS, text) except Exception as e: logging.error(e) self.write("") self.set_header("Content-Type", "image/jpg") self.write(image)
def img_captcha(request): text, image = Captcha.gene_code() # BytesIO:相当于一个管道,用来存储图片的流数据 out = BytesIO() # 调用image的save方法,将这个image对象保存到BytesIO中 image.save(out, 'png') # 将BytesIO的文件指针移动到最开始的位置 out.seek(0) response = HttpResponse(content_type='image/png') # 从BytesIO的管道中,读取出图片数据,保存到response对象上 response.write(out.read()) response['Content-length'] = out.tell() # 12Df:12Df.lower()过期时间 cache.set(text.lower(), text.lower(), 5 * 60) return response
def img_captcha(request): """ 生成4位图片验证码 :param request: :return: img """ text, image = Captcha.gene_code() out = BytesIO() image.save(out, 'png') out.seek(0) cache.set(text.lower(), text.lower(), 5*60) print('图形验证码: %s' % cache.get(text.lower())) response = HttpResponse(content_type='image/png') response.write(out.read()) response['Content-length'] = out.tell() return response
def get(self): # 获取上次保存的图片的id pre = self.get_argument('pre') # 获取本次要保存的图片id cur = self.get_argument('cur') # print("pre%s------"%pre) # print("cur%s-------"%cur) # print(Captcha.instance().generate_captcha()) # 获取一个验证码 imageName, text, imge = Captcha.instance().generate_captcha() try: if pre: self.db_redis.delete(pre) self.db_redis.setex("imge_code_%s" % cur, PIC_CODE_EXPIRES_SECONDS, text) except Exception as e: logging.ERROR(e) return self.write("") else: self.set_header("Content-Type", "image/jpg") self.write(imge)