最近有小伙伴提问:能否说下web验证的原理,感觉文字描述不清楚,于是就用代码简单的演示下:
此代码是需要依赖:
sanic19.9.0
Pillow7.0.0
import random
import string
import uuid
import base64
import platform
from PIL import Image, ImageDraw,ImageFont
from io import BytesIO
from sanic import Sanic
from sanic.response import HTTPResponse,text
from sanic.views import HTTPMethodView
app = Sanic()
session = {
}
class VerifyCode:
def __init__(self, numbers:int):
"""
指定:生成的数量
"""
self.number = numbers
def draw_lines(self, draw, num, width, height):
"""划线"""
x1 = random.randint(0, width / 2)
y1 = random.randint(0, height / 2)
x2 = random.randint(0, width)
y2 = random.randint(height / 2, height)
draw.line(((x1, y1), (x2, y2)),