python练习_简单登录
python练习_简单登录
需求:
- 做一个登录的小程序,验证用户与密码
- 要求登录三次错误后锁定
以下代码实现的功能与思路:
功能:
1、除admin以外的用户累计登录失败次数超过三次则锁定,此时需要管理员解锁。
2、当前IP地址登录失败五次则锁定IP地址一分钟后解锁。
3、admin管理员有以下管理功能 锁定用户、解锁用户、更改密码、添加用户、删除用户。
4、普通用户拥有更改密码的功能。
流程图:
思路:
1、用户信息通过ConfigParser模块进行处理并保存到文件
2、通过保存文件中counter 判断登录失败次数
3、通过socket获取当前计算机IP,如果失败把IP记录到文件中,并加入通过time获取的当前时间戳
4、管理功能通过ConfigParser模块中的一些功能处理存储用户信息的文件即可
代码Python2.7执行环境:
使用方式:
在linux上直接运行main.py即可
windows运行请把 225行解除注释,把226行注释
测试账号:
用户admin 密码:admin.123
用户test 密码:admin.123
用户test1 密码:admin.123 test1 默认锁定状态
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import time 4 import ConfigParser 5 import socket 6 import webbrowser 7 import sys 8 import getpass 9 10 # 全局变量 localIP 获取本地ip用于锁定本地ip,user_config操作用户文件句柄 11 LOCAL_IP = socket.gethostbyname(socket.gethostname()) 12 user_config = ConfigParser.ConfigParser() 13 user_config.read('userdb') 14 15 class Admin_function(object): 16 ''' 17 admin管理员功能类 18 ''' 19 def __init__(self): 20 pass 21 22 def Admin_function_main(self): 23 ''' 24 调用函数,根据输入指令调用类中的相关功能 25 ''' 26 while True: 27 print(''' 28 -------------Operation menu------------ 29 Change password:pas 30 Unlocked:lc 31 Lock the user:ul 32 Add user:in 33 Delete user:rm 34 Exit:exit 35 --------------------------------------- 36 ''') 37 admin_operation = raw_input("Please enter an action:") 38 if admin_operation == "pas": 39 self.pas_reset() 40 elif admin_operation == "lc": 41 self.counter_tag = 0 42 self.unlock_lock_user() 43 elif admin_operation == "ul": 44 self.counter_tag = 3 45 self.unlock_lock_user() 46 elif admin_operation == "in": 47 self.user_app_del = "in" 48 self.add_del_user() 49 elif admin_operation == "rm": 50 self.user_app_del = "rm" 51 self.add_del_user() 52 elif admin_operation == "exit": 53 sys.exit() 54 else: 55 print("Input Error") 56 57 def users(self): 58 ''' 59 判断admin输入用户是否存在 60 ''' 61 while True: 62 self.userlist = user_config.sections() 63 print("------------Current user-------------") 64 for single_user in self.userlist: 65 print(" %s"%single_user) 66 print("-------------------------------------") 67 self.admin_inuser = raw_input("Please enter a user:") 68 if self.admin_inuser in self.userlist: 69 break 70 else: 71 print("User does not exist") 72 73 def unlock_lock_user(self): 74 ''' 75 锁定与解锁,主要更改usedb中的counter项,如果用户输入admin则不执行操作 76 ''' 77 self.users() 78 if self.admin_inuser == "admin": 79 print("This action is not valid for admin") 80 else: 81 user_config.set(self.admin_inuser,'counter',self.counter_tag) 82 user_config.write(open('userdb', "w")) 83 if self.counter_tag == 0 : 84 print_str = "Unlock" 85 else: 86 print_str = "locking" 87 print("User %s is %s"%(self.admin_inuser,print_str)) 88 89 def pas_reset(self): 90 ''' 91 更改密码函数,用于更改用户和admin的密码,密码最小长度不能低于8位,此处可以增加 92 复杂度判断,主要更改userdb中的password字段 93 ''' 94 self.users() 95 admin_inpas = raw_input("Please enter a reset password (no less than 8 digits):") 96 if len(admin_inpas) > 7: 97 print("The password has been reset to:%s"%(admin_inpas)) 98 user_config.set(self.admin_inuser,'password',admin_inpas) 99 user_config.write(open('userdb', "w")) 100 else: 101 print("Password length is less than 8 digits Please re-enter") 102 103 def add_del_user(self): 104 ''' 105 增删用户操作,增加用户时判断用户是否存在,不存在则添加, 106 并输入设定用户密码,同样不能少于八位(可增加密码复杂度判断)对admin无效 107 删除用户时,对用户进行判断,存在则删除,对admin无效 108 ''' 109 userlists = user_config.sections() 110 print("------------Current user-------------") 111 for single_user in userlists: 112 print(" %s"%single_user) 113 print("-------------------------------------") 114 add_name = raw_input("Please enter a user:") 115 if add_name == 'admin': 116 print("This action is not valid for admin") 117 else: 118 if self.user_app_del == "in": 119 if add_name in userlists: 120 print("User already exists") 121 else: 122 while True: 123 add_inpas = raw_input("Please enter a password(no less than 8 digits):") 124 if len(add_inpas) > 7: 125 user_config.add_section(add_name) 126 user_config.set(add_name,'password',add_inpas) 127 user_config.set(add_name,'counter',"0") 128 user_config.write(open('userdb', "w")) 129 print("The[%s] user creation is complete"%add_name) 130 break 131 elif add_inpas == "exit": 132 break 133 else: 134 print("Password length is less than 8 digits Please re-enter") 135 else: 136 if add_name in userlists: 137 user_config.remove_section(add_name) 138 user_config.write(open('userdb', "w")) 139 print("[%s]User deleted"%add_name) 140 else: 141 print("User does not exist") 142 143 class user_function(object): 144 ''' 145 普通用户功能类 146 self.user 封装传入的用户 147 ''' 148 def __init__(self,inuser): 149 self.user = inuser 150 151 def user_main(self): 152 ''' 153 调用功能的函数 154 ''' 155 while True: 156 print(''' 157 -----------Operation menu----------- 158 Change password:pas 159 Exit:exit 160 ------------------------------------ 161 ''') 162 user_main_in = raw_input("Please enter an action:") 163 if user_main_in == "pas": 164 self.pas_reset() 165 elif user_main_in == "exit": 166 sys.exit() 167 else: 168 print("Input Error") 169 170 def pas_reset(self): 171 user_inpas = raw_input("Please enter a reset password (no less than 8 digits):") 172 if len(user_inpas) > 7: 173 print("The password has been reset to:%s"%user_inpas) 174 user_config.set(self.user,'password',user_inpas) 175 user_config.write(open('userdb', "w")) 176 else: 177 print("The password is less than 8 digits long") 178 179 def for_count(): 180 ''' 181 主调函数,在执行前调用ip_Locking_verify()函数,用于IP锁定验证 182 当用户登录错误次数超过五次后将锁定ip地址,一分钟后开放并获取当前时间的时间戳, 183 传入in_lock函数中;当条件正常的时候,传入用户验证函数 184 ''' 185 user_count = 0 186 while True: 187 lock_times = time.time() 188 new_lock_times = str(lock_times).split('.')[0] 189 ip_Locking_verify(new_lock_times) 190 user_certification() 191 user_count += 1 192 if user_count == 5: 193 ip_lock(new_lock_times) 194 print("Maximum login limit, see a video, please log in again after 1 minute") 195 time.sleep(1.5) 196 webbrowser.open("https://v.qq.com/x/page/i0014jfvo9j.html",new=0,autoraise=True) 197 sys.exit() 198 199 def user_certification(): 200 ''' 201 user certification(用户认证) 202 用户验证成功后,进行密码验证,admin用户不计算在次数锁定条件中 203 ''' 204 times = time.strftime("%H:%M:%S",time.localtime(time.time())) 205 user_in = raw_input("login user [%s] :"%times) 206 user_list = user_config.sections() 207 if user_in in user_list: 208 if user_in == "admin": 209 user_state = "0" 210 else: 211 user_state = user_config.get(user_in,"counter") 212 if int(user_state) < 3: 213 password_ertification(user_in) 214 else: 215 print("User is locked Please contact the administrator to unlock") 216 else: 217 print("User name wrong") 218 219 def password_ertification(user): 220 ''' 221 密码验证,失败则保持用户文件中登录次数增加1当到达三次后则锁定 222 密码验证成功后进入用户菜单,并清除登录计数 223 linux启用getpass输入密码不显示 224 ''' 225 password_in = raw_input("login password :") 226 # password_in = getpass.getpass("login password :") 227 user_password = user_config.get(user,"password") 228 if password_in == user_password: 229 user_config.set(user,'counter',0) 230 user_config.write(open('userdb', "w")) 231 if user == 'admin': 232 admin_class = Admin_function() 233 admin_class.Admin_function_main() 234 else: 235 user_class = user_function(user) 236 user_class.user_main() 237 else: 238 if user == 'admin': 239 print("Password mistake") 240 else: 241 user_counter = user_config.get(user,"counter") 242 counter_value = int(user_counter) + 1 243 user_config.set(user,'counter',str(counter_value)) 244 user_config.write(open('userdb', "w")) 245 print("Password mistake") 246 247 def ip_lock(in_lock_times): 248 ''' 249 IP锁定,触发此函数,将把ip和当前你时间戳记录到iplist文件中 250 ''' 251 with open('iplist','a') as ip_lock_in: 252 lock_in_content = "%s:%s\n"%(LOCAL_IP,in_lock_times) 253 ip_lock_in.write(lock_in_content) 254 def ip_Locking_verify(time_stamp): 255 ''' 256 验证当前ip是否被锁定,符合则解锁时间则解锁 257 通过iplist文件进行判断,程序执行时会读取现在的时间戳 258 然后减去记录在文件中的时间戳,如果大60则将条目删除,否则输出解锁时间 259 ''' 260 ip_list = [] 261 with open('iplist','rU')as main_iplist : 262 for lock_ip in main_iplist.readlines(): 263 ip_list.append(lock_ip) 264 if LOCAL_IP in lock_ip: 265 new_ip_Locking = str(lock_ip).split(':')[1] 266 if int(time_stamp) - int(new_ip_Locking) > 60: 267 ip_list.remove(lock_ip) 268 print("The current IP[%s] is unlocked"%LOCAL_IP) 269 else: 270 remaining_time = 60 - (int(time_stamp) - int(new_ip_Locking)) 271 print("The current IP[%s] is locked,Remaining unlock time [%s] seconds"%(LOCAL_IP,remaining_time)) 272 sys.exit() 273 open_iplist = open("iplist", "w+") 274 if ip_list: 275 open_iplist.writelines(ip_list) 276 else: 277 open_iplist.writelines("") 278 open_iplist.close() 279 280 if __name__ == '__main__': 281 for_count()
user存储用户数据的格式
[admin] password = admin.123 counter = 0 [test] password = admin.123 counter = 0 [test1] password = admin.123 counter = 3
创建一个叫iplist的文件,到此即可正常运行