定义有参数装饰器为被装饰函数添加认证的功能:用户信息的来源可以是文件或者ldap,三次验证失败锁定用户。
def input_msg():
'''打印用户要输入的信息,
返回用户名、密码'''
username = input("username: ")
password = input("password: ")
return username, password
def locked(username):
'''从user_times.db文件中查询用户名,
判断用户名是否已被锁定,
锁定返回False,没锁定返回True'''
flag_l = True
with open("user_times.db",mode="r",encoding="utf8") as file:
for i in file:
if username == i:
flag_l = False
if flag_l:
return flag_l
else:
return flag_l
error_name_dic = [] #存储用户名密码为错误的用户名的列表
def instert(username):
'''将用户名密码初错误的用户名插入列表,
并判该用户名在列表中是否大于等于3次,
大于等于将该用户名追加入user_times.db'''
error_name_dic.append(username)
count = 0
for i in error_name_dic:
if i == username:
count += 1
if count >= 3:
with open("user_times.db", mode="a", encoding="utf8") as file:
file.write("
")
file.write(username)
def login(username, password):
'''从user.db中判断输入用户名、
密码是够正确,正确返回True,
不正确返回False'''
flag = False
error_name = ""
with open("user.db", mode="r", encoding="utf8") as file:
for i in file:
i = eval(i)
if username == i["username"] and password == str(i["password"]):
flag = True
if username == i["username"] and password != str(i["password"]):
error_name = username
if flag:
return flag, error_name
else:
return flag, error_name
def auth_out(auth_type):
'''带参数的装饰器,返回auth'''
def auth(func):
'''@auth 等于 index = auth(index) ,返回warpper'''
def warpper(*args, **kwargs):
'''index实现用户认证'''
if auth_type == "file":
while True:
username,password = input_msg()
flag_l = locked(username)
if flag_l:
flag, error_name = login(username, password)
if flag:
func(*args, **kwargs)
return func
else:
instert(error_name)
print("login error...")
else:
print("%s had been locked." % username)
elif auth_type == "ldap":
print("We don't have ldap.")
return warpper
return auth
@auth_out(auth_type = "file")
def index():
print("Welcome,this is index page.")
index()