代码:https://github.com/Fu4ng/PythonStudy/tree/master/loginCode
用python简单的实现账户登录,账户和密码在一个TXT文件中,被锁定的账户在另一个Txt文件中。
第一次写了一个很简单的版本,
可以说这个版本bug很多了
1:不能检查是否用户名存在
2:前两次输入存在的用户名,而且密码错误,第三次输出一个不存在的用户名,那么被锁的用户是那个不存在的用户,可以利用这个BUG无限次输入密码,因为输出错误的次数是在本地,要解决这个bug,就得在用户名密码的文件中再加一个次数的数据
3:一大堆,不写了
#第一版 bug很多的
import sys
lockLimit = 3
tryTimes = 0
login = 0
data = open('user.txt')
lock_count = open('locked.txt')
while tryTimes < lockLimit:
userName = input('Your name:')
userPw = input('Your passwords:')
for line in lock_count:
if userName == line:
sys.exit()
for line in data:
line = line.split()
if userName == line[0] and userPw == line[1]:
print('login successful')
login =1
break
else:
print('sorry,your passwords not right')
tryTimes += 1
break
if login ==1:break
else:
print('locked')
addLockCount = open('locked.txt','a')
addLockCount.write(userName)
大改之后的版本
这一版我有一个不知道算不算BUG,为了省事,我把user读到内存中,这个只限小文件,如果是大文件还是老老实实把。
import sys
lockLimit = 3
user = open('user.txt', 'r+')
flag = 0
# 格式化数据
data = {}
for i in user.readlines():
i = i.strip('\n').split()
data[i[0]] = {}
data[i[0]]['pw'] = i[1]
data[i[0]]['time'] = int(i[2])
user.close()
while flag == 0:
lockCount = open('locked.txt', 'r+')
user = open('user.txt', 'r+')
userName = input('Your name:')
# 检查是否被锁
l_flag = 0
for line in lockCount.readlines():
if userName in line: l_flag = 1
if l_flag == 1:
print('%s has be locked! ' % userName)
continue
# 检查是否在用户名内
u_flag = 0
for line in user.readlines():
if userName in line: u_flag = 1
if u_flag == 0:
print('sorry,no find your name! ')
continue
# 文件指针回到开头
user.seek(0)
# 检查密码是否匹配
userPw = input('your pw :')
if data[userName]['pw'] == userPw:
print('login successfully ! \n')
flag = 1
break
else:
print('your pw worry!')
data[userName]['time'] += 1
if data[userName]['time'] >= lockLimit:
print('%s has be locked !\n ' % userName)
lockCount.write(userName)
for line in user.readlines():
line = line.strip('\n').split()
if line[0] == userName: line[2] = str(data[userName]['time'])
lockCount.close()
user.close()
if flag == 1: break
第三版,简化了很多
import sys
import fileinput
lockLimit = 3
def log(name, psw):
'登录函数'
user_info = []
with open('user.txt', 'r+') as user:
for i in user.readlines():
us, up, ut = i.strip('\n').split()
user_info = us +' '+ up+' ' + ut
if us == name and up == psw:
print('登录成功!')
return True
elif us == name and up != psw:
print('密码错误')
break
else:
continue
addTime(user_info)
def addTime(user_info):
for line in fileinput.input('user.txt',inplace=1):
print(line.replace(user_info,user_info[:-1]+str(int(user_info[-1])+1)).strip())
if int(user_info[-1])%3 >=2:
addLockedCount(user_info)
def addLockedCount(user_info):
i = str(user_info).split()
with open('locked.txt','a') as f:
f.write(i[0])
print("你的账户已被锁定!")
for line in fileinput.input('user.txt',inplace=1):
print(line.replace(user_info,user_info[:-1]+'0').strip())
def checkloked(name):
'检查账户是否被锁,被锁返回True'
with open('locked.txt', 'r') as f:
for i in f.readlines():
if name == i.strip('\n'):
print('被锁了!')
sys.exit()
name = input('你的用户名:')
pw = input('你的密码:')
checkloked(name)
f = log(name, pw)