使用python编写一个登录接口
需求:
编写登录接口
输入用户名密码
认证成功后 显示欢迎信息
输入三次后锁定
用户信息文件
黑名单的文件
黑名单里检查,不让登录
用户名密码判定
流程图:
代码:
#!/usr/bin/env python
tries = 0
lockfile = open('account_lock.txt','r+',encoding='UTF-8')
tolockfile = open('account_lock.txt','a',encoding='UTF-8')
userfile = open('account.txt','r',encoding='utf-8')
def islock(account):
for line in lockfile:
line = line.strip('\n')
if line == account:
print('此账号已锁定')
exit()
def inaccount(account):
for line in userfile:
col1_user,col2_pass = line.strip().split()
if col1_user == account:
passtries = 0
while passtries < 3:
pass1 = input('请输入密码:')
if col2_pass == pass1:
print('欢迎使用')
exit()
else:
passtries +=1
else:
tolockfile.write('%s\n' %(account))
print('账号已锁定!')
exit()
else:
print('此账号不存在')
break
while tries<3:
account = input('请输入账号:')
islock(account)
inaccount(account)
tries +=1
else:
print('此用户真的不存在 88')
exit()
lockfile.close()
userfile.close()
tolockfile.close()
# with open('account_lock.txt','a',encoding='UTF-8') as data:
# data.write("aaa")
#此方法可以方式忘记文件close
#tolockfile = open('account_lock.txt','a',encoding='UTF-8')
#open文件后进行追加至最后一行
相关文章