#! /usr/bin/env/python3 # -*- coding: utf-8 -*- #编写登录接口,输入正确显示欢迎信息,连续三次输入错误则锁定 class login(object): def __init__(self): self._database_name='log_usrdata.db' self._lockfile_name='log_usrlock.db' self.usrdata=[] self.lockusr=[] self._ncount=3 self.initial() #内部初始化操作 def initial(self): self.loaddata() #用户执行登录操作 def usrlog(self,name,password): for cur_name in self.get_lockusr(): if name==cur_name: print('%s, you are locked. Please retry 30min later.'% name) exit() while self._ncount>0: cur_check = self.check_usr(name, password) if cur_check==0: print('No data.Can\'t login.\nPlease register at first.') break elif cur_check==1: print('Welcome back %s!'% name) break else : self._ncount-=1 print('You have a wrong usrname or password. Please check it.') if self._ncount==0: self.set_lockusr(name) break name=input('Please input your name:') password=input('Please input your password:') #读取用户数据 def loaddata(self): with open('log_usrdata.db') as f: for line in f.readlines(): self.usrdata.append(line.split()) with open('log_usrlock.db') as f: for line in f.readlines(): self.lockusr.append(line) #新用户注册 def register(self,name,password): if self.get_usrdata()==[]: self.loaddata() with open('log_usrdata.db') as f: f.writelines([str(name),str(password)]) #返回用户数据 def get_usrdata(self): return self.usrdata #返回锁定用户 def get_lockusr(self): return self.lockusr #设定锁定用户 def set_lockusr(self,name): with open('log_usrlock.db','w') as f: f.writelines(str(name)) print('%s, you are locked. Please retry 30min later.'% name) #检查用户名文件是否为空 def check_file(self): cur_usrdata=self.get_usrdata() if cur_usrdata==[]: self.loaddata() cur_usrdata=self.get_usrdata() #用户为空文件 if cur_usrdata==[]: return 1 else: return 0 #检查用户与密码 def check_usr(self,name,password): if self.check_file(): return 0 for data in self.get_usrdata(): if data[0]==str(name) and data[1]==str(password): return 1 elif data[0]==str(name) and data[1]!=str(password): return 2 else: return 3 mylog=login() mylog.usrlog('test123','test1')