# python 实现生成验证码,每隔60s生成一次,输入正确退出
import random
from threading import Timer
# 定义Code类
class Code:
# 初始化时调用缓存
def __init__(self):
self.make_cache()
def make_cache(self, interval=60):
# 先生成一个验证码
self.cache = self.make_code()
print(self.cache)
# 开启定时器,60s后重新生成验证码
self.t = Timer(interval, self.make_cache)
self.t.start()
# 随机生成4位数验证码
def make_code(self, n=4):
res = ''
for i in range(n):
s1 = str(random.randint(0, 9))
s2 = chr(random.randint(65, 90))
res += random.choice([s1, s2])
return res
# 验证验证码
def check(self):
while True:
code = input('请输入验证码(不区分大小写):').strip()
if code.upper() == self.cache:
print('验证码输入正确')
# 正确输入验证码后,取消定时器任务
self.t.cancel()
break
obj = Code()
obj.check()
验证码:60s后验证码失效
最新推荐文章于 2022-06-29 17:04:58 发布