def get(self): mstream = io.BytesIO() img, code = check_code.create_validate_code() img.save(mstream, "GIF") # self.session["CheckCode"] = code # print(mstream.getvalue()) self.write(mstream.getvalue())
def get(self, *args, **kwargs): global CODE image, CODE = check_code.create_validate_code() # BytesIO操作二进制数据,将验证码图形写入内存 mstream = io.BytesIO() image.save(mstream, 'GIF') self.write(mstream.getvalue())
def get(self, *args, **kwargs): import check_code import io mstream = io.BytesIO() img, code = check_code.create_validate_code() img.save(mstream, "GIF") self.session["CheckCode"] = code print(mstream.getvalue()) self.write(mstream.getvalue())
def yanzhengma(request): import io import check_code as CheckCode stream = io.BytesIO() img, code = CheckCode.create_validate_code() img.save(stream, "png") request.session["CheckCode"] = code return HttpResponse(stream.getvalue())
def check_code(request): import io stream = io.BytesIO() # img图片对象,code在图像中写的内容 img, code = CheckCode.create_validate_code() img.save(stream, "png") # 图片页面中显示,立即把session中的CheckCode更改为目前的随机字符串值 request.session["CheckCode"] = code return HttpResponse(stream.getvalue())
def get(self): # 很多时候,数据读写不一定是文件,也可以在内存中读写,BytesIO实现了在内存中读写bytes mstream = io.BytesIO() #创建一个BytesIO,存在于内存中 #创建图片,并写入验证码 img, code = check_code.create_validate_code() #将图片对象写入到mstream中 img.save(mstream, "GIF") #为每个用户保存其验证码 self.session["check_code"] = code self.write(mstream.getvalue()) #读取图片上的内容,写到页面上
def get(self, *args, **kwargs): #生成图片并且返回,用write来返回 import io import check_code mstream = io.BytesIO() #相当于在内存里创建了个临时文件 img,code = check_code.create_validate_code() #创建图片对象,生成验证码, img.save(mstream,"GIF")# 将图片写入到io中,io是用来做输入和输出的 #mstream.getvalue()相当于读到了图片里面的内容。 self.session["Checkcode"] = code.upper() #将code大写化以后放入session,对应"Checkcode" self.write(mstream.getvalue())
def get(self, *args, **kwargs): # 创建一个容器,相当于在内存中临时创建一个文件 mstream = io.BytesIO() # 创建图片并写入验证码 img, code = check_code.create_validate_code() # 将图片写入到容器中 img.save(mstream, 'GIF') # 将Code写入到session中,为每个用户保存验证码 self.session['CheckCode'] = code # 将图片中的内容返回到页面 self.write(mstream.getvalue())
def get(self, *args, **kwargs): #生成图片并返回 import io import check_code mstream = io.BytesIO() #创建图片,并写入验证码 img, code = check_code.create_validate_code() #将图片对象写入mstream img.save(mstream, "GIF") #为每个用户保存验证码 self.session["CheckCode"] = code self.write(mstream.getvalue())
def get(self): # 创建一个文件流 mstream = io.BytesIO() # 生成图片对象和对应字符串 img, code = check_code.create_validate_code() # 将图片信息保存到文件流 img.save(mstream, "GIF") # self.session["CheckCode"] = code # 嫌麻烦存在了cookie里了 self.set_cookie('CheckCode', code) print(mstream.getvalue()) self.write(mstream.getvalue())
def get(self): # 生成图片并返回 import io, check_code # 在内存中创建容器 mstream = io.BytesIO() # 创建图片,并写入验证码 img, code = check_code.create_validate_code() # 将图片写入都IO容器中 img.save(mstream, "GIF") self.write(mstream.getvalue()) # 为每个用户保存自己的验证码 self.session['check_code'] = code
def check_code(request): """ 获取验证码 :param request: :return: """ stream = io.BytesIO() # 创建随机字符 code # 创建一张图片格式的字符串,将随机字符串写到图片上 img, code = CheckCode.create_validate_code() img.save(stream, "PNG") # 将字符串形式的验证码放在Session中 request.session["CheckCode"] = code return HttpResponse(stream.getvalue())
def get_code(n): if request.method == 'GET': #把strs发给前端,或者在后台使用session保存 code_img,strs = check_code.create_validate_code() buf = StringIO.StringIO() code_img.save(buf,'JPEG',quality=70) buf_str = buf.getvalue() response = app.make_response(buf_str) response.headers['Content-Type'] = 'image/jpeg' session['code'] = strs return response else: code = request.form['code'] if code == session['code']: return jsonify(result = True) else: return jsonify(result = False)
def get(self, *args, **kwargs): img, code = check_code.create_validate_code() #mstream = cStringIO.BytesIO() #img.save(mstream, 'GIF') img.show() print(code)
def get(self): mstream = io.BytesIO() img, code = check_code.create_validate_code() img.save(mstream, "GIF") self.write(mstream.getvalue())