#/usr/bin/env python
#yehui
'''
作业一:博客
作业二:编写登陆接口
输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定
'''
import getpass, os, pickle
class PathJudge:
def __init__(self):
self.path1 = "userinfo"
self.path2 = "user_dict_info"
def judge_path(self): ##检查文件夹是否存在
if os.path.exists('userinfo'):
print("file %s is exists and then pass" % self.path1)
else:
os.mkdir(self.path1) ##创建一级目录,如果要创建多级目录需要用到" os.makedirs('多级目录的详细路径') "
if os.path.exists('user_dict_info'):
print("file %s is exists and then pass" % self.path2)
else:
os.mkdir(self.path2)
class PickleDic:
def __init__(self):
self.pickle_file = "user_dict_info/dict_info.pickle"
def pickle_dict(self, dic): ##备份用户列表
self.dumped = dic
with open(self.pickle_file, 'wb') as self.data: ##不管备份或者读取的时候都必须要先读取该备份列表
pickle.dump(self.dumped, self.data)
def load_dict(self): ##读取用户列表
with open(self.pickle_file, 'rb') as self.load:
self.new_dict = pickle.load(self.load)
self.load.flush()
return self.new_dict
class LongIn:
def __init__(self):
self.counts = 0
self.dict = PickleDic().load_dict()
def login(self):
while self.counts < 10:
self.counts += 1
self.name = input("username: ")
self.pwd = getpass.getpass("password: ")
if self.name not in self.dict :print("用户不存在!");break
self.verify = list(self.dict[self.name].values())[0]
if self.verify >= 3:exit("你的帐号已经被锁定")
if self.pwd in self.dict[self.name].keys():
print("哥们,欢迎登上了通往未来的列车!")
break
else:
print("密码错误!")
self.verify += 1
print("你错误的次数是%s, 如错误达到3次将被锁定" % self.verify)
for self.k in self.dict[self.name].keys():
(self.dict[self.name])[self.k] = self.verify
self.back = PickleDic().pickle_dict(self.dict)
class ClearLock: ##这段代码是为了重置每天的锁定,正常情况下是跟该模块分开并且每天24点定时整执行一次
def __init__(self):
self.dict_userinfo = {'fdd': {'123': 0}, 'fgg': {'234': 0}, 'djj': {'456': 0}}
def clear_lock(self):
self.clock = PickleDic()
self.clock.pickle_dict(self.dict_userinfo)
print("锁定器清理完成! %s" % self.clock.load_dict())
if __name__ == '__main__':
path = PathJudge()
path.judge_path()
# c = ClearLock() ##如果为了是第一次执行,或者不存在用列,把这两个注释去掉.
# c.clear_lock()
l = LongIn()
l.login()