生成图形验证码
import random
import string
import uuid
from captcha.image import ImageCaptcha
redis_client = redis.StrictRedis(host=SMS_REDIS_HOST, port=SMS_REDIS_PORT, db=SMS_REDIS_DB, decode_responses=True)
def get_captcha():
captcha_id = str(uuid.uuid4())
image = ImageCaptcha()
_string = string.ascii_letters + string.digits
captcha_value = ''.join(random.sample(_string, 4))
data = image.generate(captcha_value, format="webp")
redis_client.set(captcha_id, captcha_value, 60)
return captcha_id, data.getvalue()
验证图形验证码
def verify_captcha(captcha_id, captcha_value):
value = redis_client.get(captcha_id)
if value is None or value != captcha_value:
print("验证码无效!")