Пример #1
0
 def get(self, request, image_code_id):
     """
     获取图片验证码
     :param request: 请求对象
     :param image_code_id: uuid随机值
     :return: image图片
     """
     text, image = captcha.generate_captcha()
     redis_conn = get_redis_connection('verify_codes')
     redis_conn.setex('img_%s' % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)
     return HttpResponse(content=image, content_type='image/jpg', status=status.HTTP_200_OK)
Пример #2
0
    def get(self, request, image_code_id):
        redis_conn = get_redis_connection('verify_codes')
        image_name, real_image_code, image_data = captcha.generate_captcha()
        logger.info(real_image_code)
        try:
            redis_conn.setex('CODEID_' + image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, real_image_code)
        except Exception:
            return HttpResponse({'message': '保存图片验证码失败'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # 使用Response会报错, why?
        return HttpResponse(image_data, content_type='image/JPEG')
Пример #3
0
 def get(self, request, image_code_id):
     # 1.生成验证图片image_data
     image_name, real_image_code, image_data = captcha.generate_captcha()
     # 2.保存验证图片的内容存储到redis
     try:
         redis_conn = get_redis_connection("verify_codes")
         redis_conn.setex('ImageCode_' + image_code_id, 300, real_image_code)
     except:
         Response({"message": "验证码存储有误"})
     # 3.指定响应的数据格式为'image/jpg'
     return HttpResponse(image_data, content_type='image/jpg')
Пример #4
0
    def get(self, request, image_code_id):

        # 生成验证码图片
        name, text, image = captcha.generate_captcha()

        redis_conn = get_redis_connection("verify_codes")
        redis_conn.setex("img_%s" % image_code_id,
                         constants.IMAGE_CODE_REDIS_EXPIRES, text)

        # 固定返回验证码图片数据,不需要REST framework框架的Response帮助我们决定返回响应数据的格式
        # 所以此处直接使用Django原生的HttpResponse即可
        return HttpResponse(image, content_type="image/jpg")
Пример #5
0
    def get(self, request, image_code_id):
        """
        获取图片验证码
        """
        # 生成验证码图片
        text, image = captcha.generate_captcha()
        # 保存验证码内容
        redis_conn = get_redis_connection("verify_codes")
        redis_conn.setex("img_%s" % image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)

        # 返回图片
        return HttpResponse(image, content_type="images/jpg")
Пример #6
0
    def get(self, request):
        img_url = request.get_full_path()
        image_code = re.search('image_codes/([0-9a-zA-Z\-]+)', img_url)
        # 匹配括号中的url
        image_code_id = image_code.group(1)

        # 生成验证码
        name, text, img = captcha.generate_captcha()
        # 只要保存验证码值和uuid
        redis_conn = get_redis_connection('verify_codes')
        pl = redis_conn.pipeline()
        # 设置时长
        redis_conn.setex('img_codes_%s' % image_code_id, 60, text)
        pl.execute()

        return HttpResponse(img, content_type='image/jpg')
Пример #7
0
    def get(self, request, image_code_id):

        # 3.1 生成验证码图片,验证码图片的真实值
        image_name, real_image_code, image_data = captcha.generate_captcha()
        redis_conn = get_redis_connection('verify_codes')

        # 3.2 image_code_id作为key将验证码图片的真实值保存到redis数据库,并且设置有效时长(5分钟)
        try:
            redis_conn.setex("CODEID_%s" % image_code_id,
                             constants.SMS_CODE_REDIS_EXPIRES, real_image_code)
        except Exception as e:
            return Response({'message': '数据异常'},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # 4.1 返回验证码图片(返回的数据是二进制格式,不能兼容所有浏览器)

        return HttpResponse(image_data)
Пример #8
0
def ImageVerify(request, image_code_id):
    """
    获取图片验证码
    :return:
    """

    name, text, image = captcha.generate_captcha()
    try:
        # 保存当前生成的图片验证码内容
        conn = get_redis_connection('img_code')

        conn.setex('ImageCode_' + image_code_id, 300, text)
    except Exception as e:
        return HttpResponse({'message': 'DBERROR'},
                            content_type='application/json',
                            status=400)

    return HttpResponse(image, content_type="image/png")
Пример #9
0
    def get(self, request, image_code_id):
        # 创建连接到redis的对象
        redis_conn = get_redis_connection('verify_code')

        if not image_code_id:
            logger.error("参数不足")
            return Response({'message': '微博服务器异常'}, status=status.HTTP_404_NOT_FOUND)
        image_name, real_image_code, image_data = captcha.generate_captcha()
        # 将code_id作为key将验证码图片的真实值保存到redis数据库,并设置有效时长5分钟
        try:
            redis_conn.setex("CODEID_%s" % image_code_id, 300, real_image_code)
            # print(real_image_code)
        except Exception as error:
            logger.info(error)
            return Response({'message': '验证失败'}, status=status.HTTP_400_BAD_REQUEST)
        # 返回验证码图片数据
        resp = HttpResponse(image_data)
        resp._content_type = 'image/jpg'

        return resp
Пример #10
0
    def get(self, request, image_code_id):
        """
        获取图片验证码
        :return:
        """
        # 1. 获取到当前的图片编号id
        # image_code_id = request.GET.get('image_code_id')
        # 2. 生成验证码
        name, text, image = captcha.generate_captcha()
        # print('captcha之后', text)
        try:
            # 保存当前生成的图片验证码内容
            redis_coon = get_redis_connection('image_codes')
            redis_coon.setex('ImageCode_' + image_code_id, 300, text)
        except Exception as error:
            logger.info(error)
            return Response({'errmsg': '保存图片验证码失败'},
                            status=status.HTTP_400_BAD_REQUEST)

        # 返回响应内容
        resp = HttpResponse(image)
        # 设置内容类型
        resp.content_type = 'image/jpg'
        return resp