python一个简单的登录
文件目录下有两个文件
user_name.txt
lock_file.txt
实际中可以读数据库里的信息
代码如下
1 #encoding = utf-8 2 import sys 3 4 user_file = 'user_name.txt' 5 lock_file = 'lock_file.txt' 6 7 retry_count = 0 8 retry_limit = 3 9 10 while retry_count < retry_limit: 11 username = raw_input('\033[32;1mUsername:\033[0m') 12 lock_check = file(lock_file) 13 for line in lock_check.readlines():
line = line.split() 14 if username == line[0]: 15 sys.exit('%s is locked' % username) 16 17 passwd = raw_input('\033[32;1mPassword:\033[0m') 18 19 f = file(user_file,'rb') 20 match_flag = False 21 for line in f.readlines(): 22 user,password = line.strip('\n').split() 23 if username == user and passwd == password: 24 match_flag = True 25 break 26 f.close() 27 if match_flag == False: 28 print 'User unmatched' 29 retry_count += 1 30 else: 31 print 'Welcome login Learning python' 32 sys.exit(0) 33 else: 34 print 'Your account is lock' 35 f = file(lock_file,'ab') 36 f.write(username+'\n') 37 f.close()