用户登录验证,记录一下,还需要修改黑名单不合理

 #!/usr/bin/env python3
'''
需求:编写登录接口
1、输入用户名和密码
2、验证用户密码成功后输出欢迎消息
3、3次没有验证通过锁定账户
'''
# user = {'test1':'123','test2':'234','test3':'345'}
#将文件内容转换为字典
with open('user_pass_sql', 'r')as f:
user = dict(line.strip().split(':') for line in f if line)
# print(user)#打印字典user
f.close() # print(user.keys())#打印字典user的key
# print(user.values())#打印字典user的values
user_input=input('Name:').strip()#用户输入账户
user_passwd=input('Passwd:')#用户输入密码 # fblack = open('black_list','r')#打开黑名单文件
# for len(user_input) == fblack.readlines():#判断用户是否在黑名单
# print('Account lockout!!!')
for line in open('black_list','r'):
if user_input == line.strip():
print('Account lockout!!!')
break
# fblack.close()#关闭文件
if len(user_input) == 0:#判断用户输入是否为空
print('Users cannot be null!!')
break
else:
# if list(user.keys())[a] == user_input and list(user.values())[a] == user_passwd:
if user_input in user and user_passwd == user[user_input]:#判断用户名和密码是否一致
print('Login successfully!Welcome!!!')
else:
#用户或者密码错误进入循环2次
for i in range(2):
print('User or Passwd error!!')
user_input = input('Name:').strip()
user_passwd = input('Passwd:')
if user_input in user and user_passwd == user[user_input]:#判断用户输入的用户名和密码时候和文件中匹配
print('Login successfully!Welcome!!!')
break#跳出循环
else:
black_list = open('black_list','a')#打开文件模式追加
black_list.write('\n')
black_list.write(user_input) #3次错误之后写入黑名单
black_list.close()#关闭文件
print('Account lockout!!!')

重新写了一版:

 #!/usr/bin/env python3
def black_list():
with open("black_list", "a") as black:
black.write('\n' + user_input.strip()) #将变量user_input写到文件中,\n为换行 with open('user_passwd', 'r') as f: #打开用户密码文件
user = dict(line.strip().split(":") for line in f if line)#将文件内容转换为字典 passwd_error = [] #创建密码错误列表,密码错误一次将用户写入列表
user_does_not_exist = [] #创建不存在用户列表,不存在用户写入列表 for i in range(10): #循环10次
user_input = input("Name:").strip() #输入账号,strip去除两侧空格
user_passwd = input("Passwd:").strip() #输入密码,strip去除两侧空格
with open("black_list","r") as black: #打开黑名单文件
if user_input in black: #判断用户是否在黑名单中
print("◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇\n"
"◇◇◇◇◇◇◇◇◇账户被锁定,请与管理员联系!!◇◇◇◇◇◇◇◇\n"
"◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇\n")
break #退出循环
elif len(user_input) == 0: #判断用户输入是否为空格
print("◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇\n"
"◇◇◇◇◇◇◇◇◇◇◇◇用户名不能为空!!◇◇◇◇◇◇◇◇◇◇◇\n"
"◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇\n")
continue #退出本次循环
else:
if user_input in user and user[user_input] == user_passwd:#判断用户名和密码是否一致
print("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"
"※※※※※※※※※※※※※欢迎登陆!!※※※※※※※※※※※※※※※\n"
"※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n")
break
elif user_input not in user: #判断用户是都存在
print("◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇\n"
"◇◇◇◇◇◇◇◇◇◇◇◇用户不存在,请重试!!◇◇◇◇◇◇◇◇◇◇◇◇\n"
"◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇\n")
user_does_not_exist.append(user_input) #用户不存在就写入用户不存在列表中
if user_does_not_exist.count(user_input) == 5: #统计用户不在列表中不存在的用户次数,超过5次锁定
print("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n"
"☆☆☆☆☆☆☆☆☆☆☆尝试次数过多,账户锁定!!☆☆☆☆☆☆☆☆☆☆☆\n"
"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n")
black_list() #调用函数black_list
break
else:
print("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n"
"☆☆☆☆☆☆☆☆☆☆☆☆☆密码错误!!☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n"
"☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n")
passwd_error.append(user_input) #将密码错误用户写入密码错误列表中
if passwd_error.count(user_input) == 3: #统计密码错误用户是否超过3次
print("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n"
"〓〓〓〓〓〓〓〓〓〓密码错误3次,账户锁定,请联系管理员!!〓〓〓〓〓〓〓〓〓〓\n"
"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n")
black_list() #调用函数black_list
break
else:
continue #退出当前循环
else:
print("〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n"
"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓尝试次数过多!!〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n"
"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\n")

 再修改一下:

 #!/usr/bin/env python3
def black_list():
with open("black_list", "a") as black:
black.write('\n'+user_input.strip()) #将变量user_input写到文件中,\n为换行 with open('user_passwd', 'r') as f: #打开用户密码文件
user = dict(line.strip().split(":") for line in f if line)#将文件内容转换为字典 passwd_error = [] #创建密码错误列表,密码错误一次将用户写入列表
user_does_not_exist = [] #创建不存在用户列表,不存在用户写入列表 break_flag = False #默认设置为False,用来退出多层循环
continue_flag = False
for i in range(10): #循环10次
user_input = input("Name:").strip() #输入账号,strip去除两侧空格
user_passwd = input("Passwd:").strip() #输入密码,strip去除两侧空格
with open("black_list","r") as black: #打开黑名单文件
for line in black:
if user_input == line.strip(): #判断用户是否在黑名单中
Tips = '账户%s被锁定,请与管理员联系!'%user_input #定义字符串,将用户输入变量带入字符串,%s代替用户输入
print(Tips.center(30,'*')) #字符串center方法,字符串居中,30为总长度,空白处用*代替
break_flag = True #将break_flag的值重新定义
break #退出循环
if break_flag == True: #判断break_flag的值是否为True
break
if len(user_input) == 0 or len(user_passwd) == 0: #判断用户输入是否为空格
Tips = '用户名或密码不能为空!'
print(Tips.center(30,'*'))
continue #退出本次循环
if user_input in user and user[user_input] == user_passwd:#判断用户名和密码是否一致
Tips = '欢迎用户%s登录系统'%user_input
print(Tips.center(30,'*'))
break
elif user_input not in user: #判断用户是都存在
Tips = '用户%s不存在,请重试!'%user_input
print(Tips.center(30,'*'))
user_does_not_exist.append(user_input) #用户不存在就写入用户不存在列表中
if user_does_not_exist.count(user_input) == 5: #统计用户不在列表中不存在的用户次数,超过5次锁定
Tips = '尝试次数过多,账户%s锁定!'%user_input
print(Tips.center(30,'*'))
black_list() #调用函数black_list
break
else:
Tips = '密码错误!'
print(Tips.center(30,'*'))
passwd_error.append(user_input) #将密码错误用户写入密码错误列表中
if passwd_error.count(user_input) == 3: #统计密码错误用户是否超过3次
Tips = '密码错误3次,账户%s锁定,请联系管理员!'%user_input
print(Tips.center(30,'*'))
black_list() #调用函数black_list
break
else:
continue #退出当前循环
else:
Tips = '尝试次数过多!'
print(Tips.center(30,'*'))

最新文章

  1. [WCF编程]12.事务:事务协议与管理器
  2. centos 安装ffmpeg
  3. 构造persen
  4. Manifesto – HTML5 离线应用程序缓存校验工具
  5. Engine中执行gp工具返回的要素图层如何获取?
  6. SET Statements for SQLServer
  7. ExtJS-Viewport背景图片铺满浏览器视图并自动伸缩
  8. Xshell和VirtualBox虚机CentOS7的连接
  9. 自定义连接池java.lang.ClassCastException: com.sun.proxy.$Proxy4 cannot be cast to java.sql.Connection
  10. C 语言学习准备
  11. 升级PHP版本
  12. 英语口语练习系列-C21-美式幽默
  13. mysql和mariadb支持insert delayed的问题
  14. python--yield and generator(生成器)简述
  15. Shell脚本 | 一键卸载安卓App
  16. sqli-labs 下载、安装
  17. Verilog 加法器和减法器(3)
  18. 增加图例 Legend和删除图例
  19. JBPM4.4_执行流程实例
  20. PHP 允许Ajax跨域访问 (Access-Control-Allow-Origin)

热门文章

  1. CC150相关问题
  2. 动态切换tableView中的cell的种类
  3. spider-抓取网页内容
  4. Java实例---简单的超市管理系统
  5. HTML学习---HTTP基础学习详解
  6. netstat 常用方法
  7. CentOS7.4 Keepalived+LVS 负载均衡 后台节点健康检查
  8. 深入浅出SharePoint2013——使用沙箱解决方案
  9. August 12th 2017 Week 32nd Saturday
  10. php模式设计