五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录

def f4():
d = {}
with open('a.txt', mode='rt', encoding='utf-8') as f:
for line in f:
i = line.strip('\n').split(':')
d[i[0]] = i[1]
return d

import time
def f1(x):
def f2(func):
def f3(*args, **kwargs):
while True:
flag = True
d = f4()
if d[x] == '1':
while flag:
print('时间开始')
start = time.time()
name = input('>>>:')
pwd = input('>>>:')
print('登录')
res = func(*args, **kwargs)
print('时间结束')
stop = time.time()
s = stop - start
if s < 3:
with open('a.txt', mode='at', encoding='utf-8') as f:
f.write('%s:%s\n' % (x, '0'))
flag = False
return res
else:
continue
elif d[x] == '0':
res = func(*args, **kwargs)
return res
return f3
return f2
@f1(x='count')
def index():
print('欢迎来到index')

@f1(x='count')
def home():
print('欢迎来到home')
index()
home()
posted @ 2021-07-04 16:15  点滴180  阅读(115)  评论(0编辑  收藏  举报