以下代码主要实现的是用户登录,注册,密码验证,账户锁定,重置和修改密码等功能
import pickle #第一次运行请去掉下面一段代码的注释 ''' users_db = open("users.txt","wb") users_list = [["admin","admin",1],["tiannan","123456",1],["jide","123",0]] #["admin","admin",1] -> [<用户名>,<密码>,<锁(是/否)>] pickle.dump(users_list,users_db) users_db.close() ''' users_db=open("users.txt","rb") users_list=pickle.load(users_db) users_db.close() #print(users_list) #用户登录机会 users_pass = {} for i in users_list: users_name = i[0] users_pass[users_name]=3 #users_pass = {"admin":3,"tiannan":3,"jide":3} -> {<用户名>:<可尝试登录次数>} #print(users_pass) def ha_user(user_name): no_user = "yes" for j, users_msg in enumerate(users_list): #print(i,users_msg) #判断用户名是否存在 if user_name in users_msg: no_user = j break return no_user def write_db(): users_db = open("users.txt","wb") pickle.dump(users_list,users_db) users_db.close() def change_password(): password1 = input("请输入新密码:") password2 = input("请再次输入新密码:") if password1 == password2: users_list[index][1] = password1 write_db() print("密码修改成功!") else: print("您输入的密码不一致!") while True: sel = int(input("选择:<1>登录 <2>注册 ")) if sel == 1: name = input("请输入用户名:") password = input("请输入密码:") index = ha_user(name) if index == "yes": print("用户名不存在!") else: users_info = users_list[index] #判断用户是否处于锁定状态 if users_info[-1] == 0: print("%s用户已被锁,请联系管理员解锁账户!" % users_info[0]) else: if password == users_info[1]: #重置账户锁定验证次数 users_pass[name]=3 print("登录成功!") while name == "admin": sel2 = int(input("请选择:<1>修改密码 <2>重置用户 <3>查看用户 <4>退出 ")) if sel2 == 1: change_password() elif sel2 == 2: name2 = input("请输入需要重置的用户名:") index3 = ha_user(name2) if index3 == "yes": print("用户名不存在!") else: users_list[index3][1]= "123456" users_list[index3][-1]= 1 write_db() print("%s用户重置成功!" % name2) elif sel2 == 3: name2 = input("请输入查找的用户名:") index2 = ha_user(name2) if index2 != "yes": print(users_list[index2]) else: print("该用户不存在!") elif sel2 == 4: break while name != "admin": sel3 = int(input("请选择:<1>修改密码 <2>退出 ")) if sel3 == 1: change_password() elif sel3 == 2: break else: users_pass[name]-=1 if users_pass[name] > 0: print("密码错误,你还有%d次机会,请重新登录!" % users_pass[name]) else: users_list[index][-1]=0 write_db() print("%s用户多次登录失败,已被锁,请联系管理员解锁账户!" % users_info[0]) elif sel == 2: name = input("请输入用户名:") password = input("请输入密码:") result = ha_user(name) if result == "yes": user_list = [name,password,1] users_list.append(user_list) write_db() print("注册成功!") else: print("该用户名已被注册!")
此代码仅供参考,欢迎各位博友的留言!