要校验用户是否存在,如果用户失败次数大于5次,提示用户不能登录 7 密码错误、密码输入为空error_count +1

 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';" % username
28 #         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()

 

posted @ 2021-07-02 16:09  等待iing  阅读(242)  评论(0)    收藏  举报