python一个简单的登录

时间:2023-03-09 06:02:47
python一个简单的登录

文件目录下有两个文件

user_name.txt

lock_file.txt

实际中可以读数据库里的信息

代码如下

 #encoding = utf-8
import sys user_file = 'user_name.txt'
lock_file = 'lock_file.txt' retry_count = 0
retry_limit = 3 while retry_count < retry_limit:
username = raw_input('\033[32;1mUsername:\033[0m')
lock_check = file(lock_file)
for line in lock_check.readlines():
       line = line.split()
if username == line[0]:
sys.exit('%s is locked' % username) passwd = raw_input('\033[32;1mPassword:\033[0m') f = file(user_file,'rb')
match_flag = False
for line in f.readlines():
   user,password = line.strip('\n').split()
if username == user and passwd == password:
match_flag = True
break
f.close()
if match_flag == False:
   print 'User unmatched'
retry_count += 1
else:
  print 'Welcome login Learning python'
  sys.exit(0)
else:
print 'Your account is lock'
f = file(lock_file,'ab')
f.write(username+'\n')
f.close()