實現需求以下:python
同一個用戶名連續失敗三次則鎖定,無論用戶名是否在,鎖定後在下次運行腳本登陸是一樣是鎖定狀態bash
使用文件存儲用戶名和密碼信息,與文件裏面的用戶名密碼進行認證對比app
用戶名不存在和密碼錯誤提示不能夠相同,登陸成功,帳號被禁用,密碼錯誤,用戶不存在須要有相關的提示信息ide
腳本以下:函數
#!/usr/bin/python #coding:utf8 class Login(): def userInfo(self): #將用戶名密碼信息文件處理成一個字典 with open("userinfo.txt", "r") as file: userDict = {} for i in file: userList = i.split(":") userDict.update({userList[0]:userList[1].rstrip()}) return userDict def lock_userInfo(self): #將否認的用戶的文件處理成一個列表 with open("lock_userinfo.txt", "r") as file: userList = [] for i in file: userList.append(i.rstrip()) return userList def lockUser(self, username): #若是相同用戶登陸錯誤三次就調用此函數,將用戶永久瑣定,寫入文件 with open("lock_userinfo.txt", "a") as file: file.write(username + "\n") def userLogin(self): #登陸 lockList = [] while True: username = raw_input("請輸入用戶名: ") password = raw_input("請輸入用戶密碼: ") if lockList.count(username) < 3: lock = self.lock_userInfo() user = self.userInfo() if username not in lock: if username in user: if user[username] == password: print("登陸成功") else: lockList.append(username) print("密碼錯誤") else: lockList.append(username) print("用名不存在") else: print("此用戶已禁用") else: self.lockUser(username) print("用戶登陸次數超過限制,已禁用") if __name__ == "__main__": login = Login() login.userLogin()
腳本使用方法:ip
首先須要在腳本所在的目錄下面建立兩個文件lock_userinfo.txt和userinfo.txtci
userinfo.txt存入用戶信息,一行一個用戶,用戶名和密碼用冒號分開,不要有空格,以下所示:input
lock_userinfo.txt爲被否認的用戶列表文件,初始爲空,若是有同一用戶登陸出錯三次就會被永久寫入該文件,沒法登陸,解鎖用戶就是刪除該文件裏面的用戶名
it
運行結果以下:class
正確登陸
密碼輸入錯誤三次
用戶被永久否認後再登陸
用戶名輸入錯誤屢次後
這裏是第五次才禁用,和需求有點出入,邏輯還存在一些問題,沒有想到好的方法