Ejemplo n.º 1
0
def graph_captcha():
    text, image = Captcha.gene_code()
    cache.set(key=text.lower(), value=text.lower())
    out = BytesIO()  #数据保存在内存中
    image.save(out, 'png')
    out.seek(0)  #指针归0
    resp = make_response(out.read())
    resp.content_type = 'image/png'
    return resp
Ejemplo n.º 2
0
    def get(self, request):
        uuid = request.GET.get('uuid')
        last_uuid = request.GET.get('last_uuid')
        if not uuid:
            # 参数不全,返回错误信息
            return JsonResponse({'errorcode': RET.PARAMERR, 'errormsg': "参数错误"})
        # text:验证码的文本信息   image:验证码的图片一张
        image, text = Captcha.gene_code()

        redis_conn = get_redis_connection('default')

        try:
            # 将图片的UUID存储到redis数据库中,过期时间设置为300秒
            redis_conn.set('ImageCode:' + uuid, text, settings.IMAGE_CODE_REDIS_EXPIRES)
        except Exception as e:
            logger.error(e)
            return JsonResponse({'code': RET.DBERR, 'message': "数据库错误"})

        if last_uuid is not None:
            # 删除之前的图片验证码的UUID
            redis_conn.delete('ImageCode:' + last_uuid)

        # 设置响应的内容类型为图片
        return HttpResponse(image, content_type='image/jpeg')