예제 #1
0
def create_captcha_img(self, pre_code, code):
    """01生成验证码,保存到redis"""
    if pre_code:
        self.conn.delete("captcha:%s" %pre_code)
    text, img = create_captcha()
    self.conn.setex("captcha:%s" % code, text.lower(), 60)
    return img
def create_captcha_img(self, pre_code, code):  #code是时间戳
    """01生成验证码,保存到redis"""
    if pre_code:
        self.conn.delete("captcha:%s" % pre_code)
    text, img = create_captcha()
    self.conn.setex("captcha:%s" % code, text.lower(), 60)  #三个参数分别表示:键,值,过期时间
    return img
예제 #3
0
def create_captcha_img(
        self, pre_code,
        code):  ### self参数是LoginHandler的,LoginHandler继承BaseHandler,获取redis的连接变量
    if pre_code:
        self.conn.delete("captcha:%s" % pre_code)
    text, img = create_captcha()  ##获取验证码文本内容和图片
    self.conn.setex('captcha:%s' % code, text.lower(), 60)
    return img
예제 #4
0
def create_captcha_img(self, pre_code, code):
    """生成验证码,保存到redis"""
    if pre_code:
        self.conn.delete("captcha:%s" %
                         pre_code)  #删除上一次redis缓存   存在bug  两个人同时点
    text, img = create_captcha()
    self.conn.setex("captcha:%s" % code, text.lower(), 60)  #存到redis 超过60秒消除
    return img
예제 #5
0
def create_captcha_img(self, pre_code, code):
    '''01生成验证码,把存到redis'''
    if pre_code:
        self.conn.delete("captcha:%s" % pre_code)
        # 注意你设置的redis.key是双引号就在delete()里面写双引号
    text, img = create_captcha()
    self.conn.setex('captcha:%s' % code, text, 60)
    # setex设置一个redis并且过期时间

    return img
예제 #6
0
def create_captcha_img(self, pre_code, code):
    '''
    返回一个验证码图片
        :pre_code js设置的两个属性用来重置验证码
        :code
     '''
    if pre_code:
        self.conn.delete("captcha:%s" % pre_code)
    text, img = create_captcha()
    self.conn.setex("captcha:%s" % code, text.lower(), 60)
    return img
예제 #7
0
def create_captcha_img(pre_code, code):
    """ 创建图形验证码, 保存到redis中
    :param pre_code: 上一次提交的时间戳
    :param code: 本次提交的时间戳
    :return:
    """
    if pre_code:
        conn.delete('captcha:%s' % pre_code)
    text, img = create_captcha()
    conn.setex('captcha:%s' % code, text.lower(), 60)
    return img
def create_captcha_img(self, pre_code, code):
    """创建一个生成图形验证码并将其保存在redis数据库中的函数"""
    if pre_code:
        # 当pre_code存在时,就将其删除
        self.conn.delete("captcha:%s" % pre_code)
    # 调用创建图形参数,获取text,img;其中text不用获取,可以使用"_"占字符
    # 通过导入的utils文件中的create_captcha()函数生成图形验证码
    text, img = create_captcha()
    # 在redis数据库中设置code参数:键值对和过期时间,
    # self.conn.setex("captcha:%s" % code,text,60) # 图形验证码部分不分大小写
    self.conn.setex("captcha:%s" % code, text.lower(), 60)  # 将图形验证码的英文部分都小写
    return img
예제 #9
0
def img_captcha(request):
    """03图形验证码"""
    text, image = create_captcha()

    request.session['img_captcha'] = text
    # # 将图形验证码存储在session中
    request.session.set_expiry(None)
    # #设置验证码过期时间为None

    # cache.set('captcha',text,timeout=22)
    #注意这里的key,由于django cache的机制,到redis中就变成了":1:captcha"
    #django redis cache的key是由前缀:版本号:真正的key组成
    #但是,我们获取的时候还是用cache.get('captcha')
    return HttpResponse(image, content_type='image/png')
예제 #10
0
def create_captcha(self):
    """
    在函数体内 global 声明 后的timestamp 变成了全局变量,可以修改 timestamp的值
    从而达到第一次运行是pre_code 为空的效果,当被执行第一遍时 便可以赋值
    在执行第二遍时就可以确认上一次的值时多少
    :param self:
    :return:
    1541317263
    1541317263446
    """
    global timestamp
    result = int(time())
    pre_code = timestamp
    timestamp = result
    if pre_code is not None:
        self.redis.delete("captcha")
    text, img = captcha.create_captcha()
    self.redis.setex("captcha", text, 600)
    return img