【学习园地】Flask(2)验证码

admin 2022年8月5日20:24:43评论27 views字数 4870阅读16分14秒阅读模式

【学习园地】Flask(2)验证码
【学习园地】Flask(2)验证码

【学习园地】Flask(2)验证码

1

Flask-WTF



flask_wtf是flask框架的表单验证模块,可以很方便生成表单,也可以当做json数据交互的验证工具,支持热插拔。


2
创建一个表单模板


在pycharm中安装完flask_wtf后,我们可以创建一个POST传参的模板

from flask_wtf import FlaskFormfrom wtforms import StringField, SubmitFieldfrom wtforms.validators import DataRequired

class check_codeForm(FlaskForm): verify_code = StringField('验证码', validators=[DataRequired()]) submit = SubmitField('提交')

这个模板涵盖了两个参数,一个是用户输入验证码的值,一个是用户点击提交。


3
生成验证码


导入Pillow库和random库用来生成随机数和将其变为图片

import osfrom random import randint
from PIL import Image, ImageDraw, ImageFont


随机四个字符作为验证码


def get_random_code(lls):    c = []    for i in range(lls):        s = 'QWERTYUIO123456PASDFGHJKLZXCVBNM7890'        c.append(choice(s))        print(c)    return c


建立一个画板

def generate_captcha(width=140, height=60, length=4):    img = Image.new("RGB", (width, height), (250, 250, 250))    draw = ImageDraw.Draw(img)

确定使用的字体以及大小

font = ImageFont.truetype(os.path.dirname(__file__) + '/Nightmare-Maker.ttf', size=36)

将字体进行画在画板上

cs = get_random_code(4)text = ''s = -1for i in range(length):    draw.text((width * 0.20 * (s + 1.5), height * 0.3), cs[i], font=font, fill=get_random_color())    text = text + cs[i]    s = s + 1

加入随机的线条

for i in range(30):    x1 = randint(0, 140)    y1 = randint(0, 60)
x2 = randint(0, 140) y2 = randint(0, 60) draw.line(((x1, y1), (x2, y2)),width=randint(0, 3),fill=get_random_color())

加入高斯模糊和滤镜(radius的数值不要太高)

img = img.filter(ImageFilter.GaussianBlur(radius=0.7))img = img.filter(ImageFilter.FIND_EDGES)


最终效果


【学习园地】Flask(2)验证码

这里是B9SY


完整代码


import osfrom random import randint, choice
from PIL import Image, ImageDraw, ImageFont, ImageFilter

def get_random_color(): return randint(120, 200), randint(120, 200), randint(120, 200)

def get_random_code(lls): c = [] for i in range(lls): s = 'QWERTYUIO123456PASDFGHJKLZXCVBNM7890' c.append(choice(s)) print(c) return c

def generate_captcha(width=140, height=60, length=4): img = Image.new("RGB", (width, height), (250, 250, 250)) draw = ImageDraw.Draw(img)
font = ImageFont.truetype(os.path.dirname(__file__) + '/Nightmare-Maker.ttf', size=36)
cs = get_random_code(4) text = '' s = -1 for i in range(length): draw.text((width * 0.20 * (s + 1.5), height * 0.3), cs[i], font=font, fill=get_random_color()) text = text + cs[i] s = s + 1

for i in range(30): x1 = randint(0, 140) y1 = randint(0, 60)
x2 = randint(0, 140) y2 = randint(0, 60) draw.line(((x1, y1), (x2, y2)),width=randint(0, 3),fill=get_random_color())
img = img.filter(ImageFilter.GaussianBlur(radius=0.7)) img = img.filter(ImageFilter.FIND_EDGES)    print(text)
    return text, img



generate_captcha()



4
实装


设计一个初始页面来发放cookie和建立字典来存放验证码


@app.route('/')def setcooke():    resp = make_response("cookie以下发")    ussww = md5(str(time()).encode('utf-8')).hexdigest()    resp.set_cookie("userid", ussww)    users_info[ussww] = ['ON', 'NULL']    return resp


建立一个页面来检查验证码

@app.route('/check_code', methods=['GET', 'POST'])def check_code():    form = check_codeForm()    resp = make_response(render_template('check_code.html', form=form))    cookes = request.cookies.get('userid')
if request.method == 'POST': if users_info[cookes][1] != str(form.verify_code.data).replace('n', ''): print(users_info[cookes][1]) print("不相等") if users_info[cookes][1] == str(form.verify_code.data).replace('n', ''): print("相等")
return resp

检查POST传参

if request.method == 'POST':    if users_info[ussww][1] != str(form.verify_code.data).replace('n', ''):        print("不相等")    if users_info[ussww][1] == str(form.verify_code.data).replace('n', ''):        print("相等")

再写一个生成验证码的页面

@app.route('/codes')def chek_doe():    cookes = request.cookies.get('userid')    users_info[cookes][1], img = yanzhengma.generate_captcha()    buffer = BytesIO()    img.save(buffer, "JPEG")    buf_bytes = buffer.getvalue()    response = make_response(buf_bytes)    response.headers['Content-Type'] = 'image/jpg'    return response

这里将获取到了验证码存到cookie对应的字典里,然后图片放入内存并作表头返回


输入验证码并检测的页面

<form action="/check_code" method="post">    {{ form.verify_code.label }}{{ form.verify_code }}    {{ form.submit }}    <img  src="{{url_for('chek_doe')}}"></form>



测试效果


【学习园地】Flask(2)验证码


【学习园地】Flask(2)验证码



【学习园地】Flask(2)验证码



整体代码


from datetime import timefrom hashlib import md5from io import BytesIO
from flask import Flask, render_template, request, make_response
import configimport yanzhengmafrom key_code_form import check_codeForm
app = Flask(__name__)app.config["SECRET_KEY"] = '123456'app.config.from_object(config)app.debug = Falseusers_info = {}
@app.route('/')def setcooke(): resp = make_response("cookie下发") ussww = md5(str(time()).encode('utf-8')).hexdigest() resp.set_cookie("userid", ussww) users_info[ussww] = ['ON', 'NULL'] return resp




@app.route('/check_code', methods=['GET', 'POST'])def check_code(): form = check_codeForm() resp = make_response(render_template('check_code.html', form=form)) cookes = request.cookies.get('userid')
if request.method == 'POST': if users_info[cookes][1] != str(form.verify_code.data).replace('n', ''): print(users_info[cookes][1]) print("不相等") if users_info[cookes][1] == str(form.verify_code.data).replace('n', ''): print("相等")
return resp

@app.route('/codes')def chek_doe(): cookes = request.cookies.get('userid') users_info[cookes][1], img = yanzhengma.generate_captcha() buffer = BytesIO() img.save(buffer, "JPEG") buf_bytes = buffer.getvalue() response = make_response(buf_bytes) response.headers['Content-Type'] = 'image/jpg' return response

if __name__ == '__main__': app.run(host="0.0.0.0",port=5555)




大学生网络安全尖锋训练营

邮箱|[email protected] 
地址|北京市海淀区奥北科技园20号楼5层

【学习园地】Flask(2)验证码





原文始发于微信公众号(大学生网络安全尖锋训练营):【学习园地】Flask(2)验证码

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年8月5日20:24:43
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【学习园地】Flask(2)验证码https://cn-sec.com/archives/1224422.html

发表评论

匿名网友 填写信息