需求:
1、输入用户名、密码
2、认证通过欢迎
3、三次失败锁定用户
一:
#!/usr/bin/env python
import os #导入模块
account_file = 'account.txt' #用户文件
lock_file = 'lock.txt' #锁定用户文件
for i in range(3):
username = raw_input("username:").strip() #消除空格,包括\n\r\t
password = raw_input("password:").strip()
if len(username) !=0 and len(password) !=0: #判定非空
f = file(account_file)
loginSuccess = False #置标志位
for line in f.readlines():
line = line.split() #切
if username == line[0] and password ==line[1]:
# user and passwd are correct
print "Welcome %s login my system" % username
loginSuccess = True
break
if loginSuccess is True: # login success
break
else:
continue
else:
f = file(lock_file)
f.write('%s\n' % username) #写入锁定文件
f.close()
二:
#!/usr/bin/env python
import os
account_file = 'account.txt'
lock_file = 'lock.txt'
#put account in a list
f = file(account_file)
acount_list = f.readlines()
f.close()
while True:
#put locked user into a lock list
f = file(lock_file)
lock_list = []
for i in f.readlines():
line = i.strip('\n') #rm \n
lock_list.append(line ) #追加
f.close()
print lock_list
loginSuccess = False
username = raw_input('user:').strip()
if username in lock_list:
print "sorry"
break
for line in acount_list:
line = line.split()
if line[0] == username: #correct name
for i in range(3):
password = raw_input('passwd:').strip()
if password == line[1]: # correct pd
print "Welcome %s login my system!@" % username
loginSuccess = True
break
else:
f = file(lock_file,'a')
f.write('%s\n' %username)
f.close()
print "Enter 3 times of wrong passwd,going to lock %s" % username
if loginSuccess == True:break # jump out of for top loop
if loginSuccess == True:break # jump out of while loop