Python使用Timer实现验证码功能
2024-09-28 19:57:22
from threading import Timer
import random class Code:
def __init__(self):
self.make_cache() def make_cache(self,interval =15):#生成的验证码保存在cache里,过15s重新生成验证码,更新cache
self.cache = self.make_code()
print(self.cache)
self.t = Timer(interval,self.make_cache)
self.t.start() 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()
最新文章
- sqlite的增删改查
- javascript获得客户端IP的又一方法
- sprinvMVC路径拦截
- CLR via C#(09)-扩展方法
- java对象equals方法的重写
- 数往知来 ADO.NET <;八>;
- 【转】SQL2008清除日志
- [二分匹配]URAL1721Two Sides of the Same Coin
- GET——token
- Oracle table names are case sensitive (normally all uppercase)
- 关于css的text-indent首行缩进两个字符和图片缩进的问题
- js常用 弹出确认 取消对话框
- [模板] tarjan/联通分量/dfs树
- Django+Vue打造购物网站(五)
- 吴裕雄 python 数据可视化
- JavaScript机器学习之KNN算法
- ADSL 动态IP拨号VPS 软件配置
- PHP实现简单倒计时
- leetcode 300最长上升子序列
- F4+2 团队项目软件设计方案