示例#1
0
文件: views.py 项目: cjmonkey/django
def img_captcha(request):
    text,image = Captcha.gene_code()

    # image,需要放到流中放回,不能放到HTTPResponse中
    # BytesIO相当于一个管道,用来存储图片的流数据
    out = BytesIO()

    # 将数据写到BytesIO,类型是png
    image.save(out, 'png')

    # 移动文件指针
    out.seek(0)

    response = HttpResponse(content_type='image/png')

    # 从BytesIO中将数据读取出来,保存到 response 对象上
    response.write(out.read())

    # out.tell()获取当前指针的尾椎,就能知道文件大小
    response['Content-length'] = out.tell();

    # 保存到memcached中
    cache.set(text.lower(), text.lower(), 60*5);

    return response
示例#2
0
 def get(self, 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()
     cache.set(text.lower(), text.lower(), ex=300)
     return response
示例#3
0
文件: views.py 项目: qileDai/xfz
def image_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()

    return response
示例#4
0
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()
    cache.set(text.lower(), text.lower(), 10 * 60)
    return response
示例#5
0
def img_captcha(request):
    text, image = Captcha.gene_code()
    out = BytesIO()  # ByteIO用于存储字节
    image.save(out, 'png')  # 将验证码图片暂存于ByteIO
    out.seek(0)  # 将ByteIO读写指针归零

    resp = HttpResponse(content_type='image/png')
    resp.write(out.read())  # 将ByteIO中的字节读取并写给HttpResponse
    resp['Content-length'] = out.tell()

    cache.set(text.lower(), text.lower(), 5 * 60)  # 缓存验证码
    return resp
示例#6
0
文件: views.py 项目: cwj214228/Blog
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()
    # 以验证码的小写为key,验证码的小写为value,保存再服务器的缓存中2分钟
    cache.set(text.lower(), text.lower(), 2 * 60)
    return response
示例#7
0
文件: views.py 项目: lizhaoqwe/myrepo
def img_captcha(request):
    text,image = Captcha.gene_code()
    # BytesIO相当于一个管道,用来存储图片的流数据
    out = BytesIO()
    # 调用image的save方法,将照片存储到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()
    return response
示例#8
0
文件: views.py 项目: fanyi055578/xfz
def image_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())
    print(text)
    cache.set(text.lower(),text.lower(), 5*60)
    captcha.append(text)

    response['Content-length'] = out.tell()
    return response
def img_captcha(request):
    """图形验证码"""
    text, image = Captcha.gene_code()
    # 通过BytesIO(), 存储图片的流数据
    out = BytesIO()
    # 调用image的save(),将 image 对象保存到 BytesIO 中
    image.save(out, "png")
    # 将 文本指针 移动到最开始的位置
    out.seek(0)
    response = HttpResponse(content_type="image/png")
    response.write(out.read())
    response["Content-length"] = out.tell()
    cache.set(text.lower(), text.lower(), 10*60)
    return response
示例#10
0
文件: views.py 项目: Fiy02/xfz_demo
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()
    cache.set(text.lower(), text.lower(), 5 * 60)
    return response
示例#11
0
def img_captcha(request):

    text, image = Captcha.gene_code()
    out = BytesIO()  # save the image stream
    image.save(out, 'png')
    out.seek(0)  # move the out pointer to start position
    response = HttpResponse(content_type='image/png')
    response.write(out.read())  # read image stream from ByteIO pipe
    response['Content-length'] = out.tell()  # size of the image

    # key: value = text.lower(): text.lower()
    cache.set(text.lower(), text.lower(), 5 * 60)

    return response
示例#12
0
def img_captcha(reauest):
    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()
    cache.set(text.lower(), text.lower(), 5 * 60)
    return response
示例#13
0
def img_captcha(request):
    text, image = Captcha.gene_code()
    #将图片存入字节流
    out = BytesIO()
    image.save(out, 'png')
    #将Bytes文件指针移动到开始
    out.seek(0)
    response = HttpResponse(content_type='image/png')
    #从BytesIo中读取图片数据1,存到response上
    response.write(out.read())
    response['Content-length'] = out.tell()
    #将图形验证码存入memcached
    cache.set(text.lower(), text.lower(), 5 * 60)

    return response
示例#14
0
文件: views.py 项目: Rousong/xfz
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())
    cache.set(text.lower(), text.lower(), 300)
    # 测试时打印图形验证码使用
    #print(f'图形验证码:{text.lower()}')
    return response
示例#15
0
def img_captcha(request):
    # 图片验证码
    text, image = Captcha.gene_code()
    out = BytesIO()  # BytesIO相当于一个管道,用类似储存图片的流数据
    image.save(out, 'png')  # 调用image的save方法,将这个image对象保存到BytesIO中
    out.seek(0)  # 将BytesIO的文件指针一道最开始的位置

    response = HttpResponse(content_type='image/png')
    #  从BytesIO管道中,读取出图片数据,保存到response对象上
    response.write(out.read())
    response['Content-length'] = out.tell()  # 读取图片大小

    cache.set(text.lower(), text.lower(), 5 * 60)

    return response
示例#16
0
def img_captcha(request):
    text, image = Captcha.gene_code()
    # BytesIO 相当于数据流管道, 用来存储图片的流数据
    out = BytesIO()
    # 将 image 保存到 BytesIO 管道里
    image.save(out, 'png')
    # 保存结束后,BytesIO文件指针自动指向数据流 末尾, 但是后面要读取数据流,所以要让指针指向开头 0 处
    out.seek(0)

    # 设置缓存,用于验证
    cache.set(text.lower(), text.lower(), 50 * 60)

    response = HttpResponse(content_type='image/png')
    response.write(out.read())
    response['Content_length'] = out.tell()  # 获取文件的长度
    return response
示例#17
0
文件: views.py 项目: qDonl/xfz_front
def img_captcha(request):
    # 图片验证码 视图函数
    text, image = Captcha.gene_code()
    cache.set(text.lower(), text.lower(), 5 * 60)
    print(f"图形验证码: {text}")
    # BytesIO 相当于一个管道, 用于存储图片
    out = BytesIO()
    image.save(out, 'png')
    # 设置数据流的读取方式为: 从一开始开始读(默认情况下, 读取数据后seek会往后跑)
    out.seek(0)
    response = HttpResponse(content_type='image/png')
    # 从数据流中读取图片数据
    response.write(out.read())
    # 文件长度 = 当前数据流从开始到 读取完后的位置(也就是数据流的长度)
    response['Content-length'] = out.tell()
    return response
示例#18
0
文件: views.py 项目: NAZBIN/Web
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()
    #把验证码存储在缓存系统中
    cache.set(text.lower(),text.lower(),5*60) #过期时间五分钟
    #把小写验证码作为key并把这个value存储起来

    return response
示例#19
0
文件: views.py 项目: iamsnn/showsite
def image_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()

    #字典型
    cache.set(text.lower(), text.lower(), 5 * 60)

    return response
示例#20
0
def img_captcha(request):
    #第一步 实例化对象 调用
    text, image = Captcha.gene_code()
    #因为 图片不能直接放到  HttpResponse 直接返回

    out = BytesIO()  #可以先放到管道中 BytesIO相当于一个管道 存放图片流数据
    image.save(out, 'png')  #调用image save方法 将图片流数据保存在BytesIO中 指定图片为png

    out.seek(0)  #将指针回0
    response = HttpResponse(content_type="image/png")  #指定返回内容的类型
    response.write(out.read())  #从管道中读取数据  放到 HttpResponse中
    #这个地方如果 管道指针 不回  0 它会从最后的位置开始往后读  会反回空
    #所以上一步将指针回0
    response['Content-length'] = out.tell()  #也就是返回指针的位置

    cache.set(text.lower(), text.lower(), 300)  #把图形验证码放到缓存中
    return response
示例#21
0
文件: views.py 项目: liudianjun/xfz
def get_captcha(request):
    text, image = Captcha.gene_code()
    out = BytesIO()
    # 将image对象保存到out中
    image.save(out, 'png')
    # 把指针的位置移动到开始的位置
    out.seek(0)
    response = HttpResponse(content_type='image/png')
    # 读取图片数据 保存到response中
    response.write(out.read())
    # 设置图片大小
    response['Content-length'] = out.tell()

    # 12Df 12df
    cache.set(text.lower(), text.lower(), 5 * 60)

    return response
示例#22
0
文件: views.py 项目: zuming001/kxyz
def img_captcha(request):
    text, image = Captcha.gene_code()
    # image必须借助流,需要用到BytesIO对象,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
示例#23
0
def img_captcha(request):
    text, image = Captcha.gene_code()
    # 存储图片数据流
    out = BytesIO()
    # 将图片对象保存到BytesIO中
    image.save(out, 'png')
    # 将文件指针移到开头
    out.seek(0)

    response = HttpResponse(content_type='image/png')
    response.write(out.read())
    # .tell(),获得文件指针的位置,也就是文件长度
    response['Content-length'] = out.tell()

    # 将图形验证码的文字存储到memcached中,全部统一为小写形式
    cache.set(text.lower(), text.lower(), 5*60)
    return response
示例#24
0
def img_captcha(request):
    text, image = Captcha.gene_code(
    )  #从验证码类的类方法 获取text和 image  不能直接放到HttpResponse返回
    #因为是图片 也就是流数据 需要单独的存储
    out = BytesIO()
    #调用image的save方法  将image对象保存在BytesIO
    image.save(out, 'png')

    #当image对象进入BytesIO后 指针从0 到1 后期需要读取保存到response对象上面 如果从1往后读,肯定读取不到1
    #需要从指针播到0
    out.seek(0)
    response = HttpResponse(content_type='image/png')
    response.write(out.read())  #从BytesIO读取数据  保存到response对象上
    response['Content-length'] = out.tell()
    cache.set(text.lower(), text.lower(), 5 * 60)
    print(cache.get(text.lower()))
    return response
示例#25
0
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())
    # 获取文件长度,out.tell()文件读取后指针移动到尾部,获取指针长度就是文件的大小
    response['Content-length'] = out.tell()
    # 将验证码存储在memcache中(小写,过期时间)
    cache.set(text.lower(), text.lower(), 5 * 60)
    return response
示例#26
0
def img_captcha(request):
    text, image = Captcha.gene_code()
    # BytesIO:相当于一个管道,用来存储图片的流数据
    out = BytesIO()
    # 调用image的save方法,将这个image对象保存到BytesIO中
    image.save(out, 'png')
    #将BytesIO的文件指针移动到最开始的位置
    out.seek(0)

    #HttpResponse默认存储字符串,现在改为图片png格式
    response = HttpResponse(content_type='image/png')

    #从BytesIO的管道中,读取图片数据,保存到response对象中
    response.write(out.read())
    # out.tell()--获取当前文件指针的位置--即告诉文件的大小
    response['Content-length'] = out.tell()
    # 设置memcahed
    cache.set(text.lower(), text.lower(), 5 * 60)
    return response
示例#27
0
def img_captcha(request):
    """
    将图片验证码放入memcache中
    """
    text, image = Captcha.gene_code()
    print(text)
    # 用于存储图片的流
    out = BytesIO()
    # 将image对象保存到out流中。
    image.save(out, 'png')
    # 移动光标到最前面
    out.seek(0)
    response = HttpResponse(content_type="image/png")
    response.write(out.read())
    # 获取当前指针的位置,同时也是当前文件的大小
    response['Content-Length'] = out.tell()
    # 将图片验证码存到memcached中
    cache.set(text.lower(), text.lower(), 10 * 60)
    return response
示例#28
0
文件: views.py 项目: jangocheng/xfz
def img_chptcha(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())
    # 上一句out.read()执行完之后,指针走到文件最后的位置,那么out.tell()是获取当前位置,自然就是文件的大小(字节数)
    response['Content-length'] = out.tell()

    # 12Df:12Df.lower()
    cache.set(text.lower(), text.lower(), 5*60)
    # CACHE.set(text.lower(), text.lower(), ex=5*60)

    return response
示例#29
0
文件: views.py 项目: lizhaojiang/xfz
def img_captcha(request):
    # 生成验证码内容和图片
    text, image = Captcha.gene_code()
    # 返回图片,response无法返回图片,需要使用io中的BytesIO
    out = BytesIO()
    # 将图片保存到数据流,并且需要定义返回类型
    image.save(out, 'png')
    # 将文件指针移动到最开始的位置
    out.seek(0)
    # 指定返回的类型为图片类型, 大类/小类
    response = HttpResponse(content_type='image/png')
    # 从BytesIO管道中读取图片数据,保存到response中
    response.write(out.read())
    # 获取文件指针的位置
    response['Content-length'] = out.tell()

    # 将生成的验证码保存到memcached缓冲中
    # 参数分别是 key value expiry
    cache.set(text.lower(), text.lower(), 5 * 60)

    # 返回response
    return response
示例#30
0
文件: views.py 项目: bujidaoei/xfz
def img_captcha(request):
    # 获取生成的验证码和图片
    text,image = Captcha.gene_code()
    # image对象并不能直接放在HttpResponse中返回,得放到流(要借助I/O的对象)当中进行返回
    # BytesIO:相当于一个管道,用来存储图片的流数据
    out = BytesIO()
    # 调用save方法,将这个image保存到BytesIO对象中(且图片类型为png)
    image.save(out,'png')
    # 此时out就在文件指针的最后一个位置了
    # 将BytesIO的文件指针移动到最开始的位置
    out.seek(0)
    # HttpResponse默认是存储字符串,这里指定HttpResponse存储的类型是png的图片
    response = HttpResponse(content_type='image/png')
    # 从BytesIO的管道中,读取出图片数据,保存到response对象上
    # 因为这里要out.read(),这也是为什么前面把文件指针移动到最开始的位置
    response.write(out.read())
    # 文件指针从开头移到最后,此时out.tell()就可以代表文件的大小
    response['Content-length'] = out.tell()
    # 将图形验证码存储到缓存中(memchched)
    # 比如验证码为12Df,那就将12Df.lower()存储到memchched中(为了用户体验,不区分大小写)
    # 以字典方式存储,字典的key和value都是text.lower(),并且设置过期时间为5分钟
    cache.set(text.lower(),text.lower(),5*60)
    return response