要校驗使用者是否存在,如果使用者失敗次數大於5次,提示使用者不能登入 7 密碼錯誤、密碼輸入為空error_count +1
阿新 • • 發佈:2021-07-02
1 """ 2 3、登入 3 username 4 password 5 6 要校驗使用者是否存在,如果使用者失敗次數大於5次,提示使用者不能登入 7 密碼錯誤、密碼輸入為空error_count +1 8 """ 9 10 import tools 11 import string 12 # 13 # for i in range(3): 14 # username = input("username:").strip().lower() 15 # password = input("password:").strip()16 # if len(username) == 0 or len(password) == 0: 17 # print("賬號/密碼不能為空") 18 # elif len(username) < 5 or len(username) > 10: 19 # print("使用者名稱長度在5-10之間") 20 # elif len(password) not in range(6, 13): # 6 7 8 9 10 11 12 21 # print("密碼的長度要在6-12直接") 22 # elif not (set(password) & set(string.digits) and set(password) & \23 # set(string.ascii_uppercase) and set(password) & set(string.ascii_lowercase) \ 24 # and set(password) & set(string.punctuation)): 25 # print("密碼複雜度不對") 26 # else: 27 # query_user_sql = "select * from nhy_user where username='%s';" % username28 # user_info = tools.op_mysql(query_user_sql, all=False, data_type=2) 29 # if not user_info: 30 # print("使用者不存在") 31 # elif user_info.get("error_count") > 4: 32 # print("錯誤次數過多") 33 # elif user_info.get("password") == tools.my_md5(password): 34 # print("登入成功!") 35 # break 36 # else: 37 # update_user_error_count = "update nhy_user set error_count=error_count+1 where username='%s';" % username 38 # tools.op_mysql(update_user_error_count) 39 # print("密碼錯誤!") 40 # else: 41 # print("錯誤次數達到上限!") 42 43 44 def check_data(username, password): 45 if len(username) == 0 or len(password) == 0: 46 print("賬號/密碼不能為空") 47 elif len(username) < 5 or len(username) > 10: 48 print("使用者名稱長度在5-10之間") 49 elif len(password) not in range(6, 13): # 6 7 8 9 10 11 12 50 print("密碼的長度要在6-12直接") 51 elif not (set(password) & set(string.digits) and set(password) & \ 52 set(string.ascii_uppercase) and set(password) & set(string.ascii_lowercase) \ 53 and set(password) & set(string.punctuation)): 54 print("密碼複雜度不對") 55 else: 56 return True 57 58 59 def check_db(username, password): 60 query_user_sql = "select * from nhy_user where username='%s';" % username 61 user_info = tools.op_mysql(query_user_sql, all=False, data_type=2) 62 if not user_info: 63 print("使用者不存在") 64 elif user_info.get("error_count") > 4: 65 print("錯誤次數過多") 66 elif user_info.get("password") == tools.my_md5(password): 67 print("登入成功!") 68 return True 69 else: 70 update_user_error_count = "update nhy_user set error_count=error_count+1 where username='%s';" % username 71 tools.op_mysql(update_user_error_count) 72 print("密碼錯誤!") 73 74 def main(): 75 for i in range(5): 76 username = input("username:").strip().lower() 77 password = input("password:").strip() 78 if not check_data(username,password): 79 continue 80 if check_db(username,password): 81 break 82 else: 83 print("錯誤次數過多") 84 85 86 if __name__ == '__main__': 87 main()