通过文件读写,条件循环相关语法,实现三次登录失败则锁定该账号的功能

需求一

"""
需求描述:
1.输入正确账号,密码,退出程序
2.登录失败,重新输入账号密码
3.同一账号连续登录失败超过三次,则锁定该账号
4.登录已锁定账号,提示账号已锁定
"""

流程图一

实例代码一

#!/usr/bin/python
#coding=utf-8 """
需求描述:
1.输入正确账号,密码,退出程序
2.登录失败,重新输入账号密码
3.同一账号连续登录失败超过三次,则锁定该账号
4.登录已锁定账号,提示账号已锁定
user.txt:
Milton Loveyp 0
Cherish Loveypgfc 0
yangp Loveypgfc 0
""" def modify_user(name, mode="add"):
"""
修改用户名单中的登录错误次数
:param name: 用户名称
:param mode:add,登录错误次数自增1;del,登录错误次数重置为0
:return:
""" # 读取用户文件
with open("user.txt", "r") as read_file:
read_data = read_file.readlines() # 写入用户文件
with open("user.txt", "w") as write_file:
for line in read_data:
new_line = line.split()
if new_line[0] == name:
if mode == "add":
new_line[2] = bytes(int(new_line[2]) + 1)
else:
new_line[2] = ""
new_line = " ".join(new_line) + "\n"
write_file.write(new_line)
write_file.flush()
else:
write_file.write(line) def auth(name, passwd):
"""
用户鉴权
:param name: 登录用户名
:param passwd: 登录密码
:return:
"""
with open("user.txt", "r") as read_file:
for eachLine in read_file:
user_name, password, errors = eachLine.split()
if name == user_name:
if int(errors) < 3:
if password == passwd:
print "Dear [%s],welcome~" % name
modify_user(name, "del")
return True
else:
print "auth fail,please check your account and password!"
modify_user(name, "add")
return False
else:
print "your accout is locked!"
return False
else:
print "your account is not exist!"
return False def login():
"""
登录
:return:
"""
while True:
name = raw_input("Name:")
passwd = raw_input("PASS:")
if auth(name, passwd):
break if __name__ == '__main__':
login()

需求二,在一的基础上添加锁定时间控制。

"""
需求点:
1.用户输入账号与密码
2.如果输入正确,则提示欢迎信息
3.如果输入账号不存在,则提示账号不存在
4.如果输入账号与密码不匹配,提示验证失败
5.同一账号,如果连续输入三次错误密码,则锁定该账号2min,2min后重新输入,如果账号密码匹配,则登录成功
6.如果三次登录内成功登录,则清空登录错误次数
7.登录时,如果账号已被锁定,提示账号已锁定信息
"""

流程图二

实例代码二

#!/usr/bin/env python
# -*- coding: utf-8 -*- """
需求点:
1.用户输入账号与密码
2.如果输入正确,则提示欢迎信息
3.如果输入账号不存在,则提示账号不存在
4.如果输入账号与密码不匹配,提示验证失败
5.同一账号,如果连续输入三次错误密码,则锁定该账号2min,2min后重新输入,如果账号密码匹配,则登录成功
6.如果三次登录内成功登录,则清空登录错误次数
7.登录时,如果账号已被锁定,提示账号已锁定信息
user.txt:
Milton Loveyp 0
Cherish Loveypgfc 0
yangp Loveypgfc 0
""" import time def modify_user(name, mode="add"):
"""
修改用户名单中的登录错误次数
:param name: 用户名称
:param mode:add,登录错误次数自增1;del,登录错误次数重置为0
:return:
"""
# 读取用户文件
with open("user.txt", "r") as read_file:
read_data = read_file.readlines() # 写入用户文件
with open("user.txt", "w") as write_file:
for line in read_data:
new_line = line.split()
if new_line[0] == name:
if mode == "add":
new_line[2] = bytes(int(new_line[2]) + 1)
if len(new_line) < 4:
new_line.append(str(time.time()))
else:
new_line[3] = str(time.time())
else:
new_line[2] = ""
new_line = new_line[0:3]
new_line = " ".join(new_line) + "\n"
write_file.write(new_line)
write_file.flush()
else:
write_file.write(line) def auth(name, passwd, lock_time):
"""
用户鉴权
:param name: 登录用户名
:param passwd: 登录密码
:return:
"""
with open("user.txt", "r") as read_file:
for eachLine in read_file:
user_list = eachLine.split()
if len(user_list) < 4:
user_list.append(str(time.time() - 100000))
user_name, password, errors, last_time = user_list if name == user_name:
if password == passwd:
if time.time() - float(last_time) > lock_time or int(errors) < 3:
print "Dear [%s],welcome~" % name
modify_user(name, "del")
return True
else:
print "your accout is locked,please try after [%f]s !" % (
lock_time - (time.time() - float(last_time)))
return False
else:
if int(errors) < 3:
print "auth fail,please check your account and password!"
modify_user(name, "add")
return False
elif time.time() - float(last_time) > lock_time:
print "auth fail,please check your account and password!"
modify_user(name, "del")
modify_user(name, "add")
return False
else:
print "your accout is locked,please try after [%f]s !" % (
lock_time - (time.time() - float(last_time)))
return False
else:
print "your account is not exist!"
return False def login():
"""
登录
:return:
"""
while True:
user_name = raw_input("Name:")
pwd = raw_input("PASS:")
if auth(user_name, pwd, 120):
break if __name__ == '__main__':
login()

  

Python 文件读写,条件循环(三次登录锁定账号实例)的更多相关文章

  1. python文件读写小结

    读文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): >>> f = open('test.txt', 'r') r表示是文本文件,rb是二进制文件 ...

  2. python文件读写及形式转化和CGI的简单应用

    一丶python文件读写学习笔记 open() 将会返回一个 file 对象,基本语法格式如下: open(filename, mode) filename:包含了你要访问的文件名称的字符串值. mo ...

  3. python 文件读写操作(24)

    以前的代码都是直接将数据输出到控制台,实际上我们也可以通过读/写文件的方式读取/输出到磁盘文件中,文件读写简称I/O操作.文件I/O操作一共分为四部分:打开(open)/读取(read)/写入(wri ...

  4. python 文件读写方式

    一.普通文件读写方式 1.读取文件信息: with open('/path/to/file', 'r') as f: content = f.read() 2.写入文件中: with open('/U ...

  5. Python文件读写及网站显示

    一.关于文件读写的笔记 (一) 文件概述 文件是一个存储在辅助存储器上的数据序列,可以包含任何数据内容 文件都是按照2进制进行存储的,但在表现形式上有2种:文本文件和二进制文件. 1. 文本文件 文本 ...

  6. python文件操作和集合(三)

    对文件的操作分三步: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 文件基本操作:         f = open('file.txt','r') #以 ...

  7. Python 简明教程 --- 24,Python 文件读写

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 过去的代码都是未经测试的代码. 目录 无论是哪种编程语言,IO 操作都是非常重要的部分.I 即Inp ...

  8. 【学习】python文件读写,用with open as的好处,非常好【转载】

    原文链接:http://www.cnblogs.com/ymjyqsx/p/6554817.html 备注:博主还有很多值得学习的笔记,遇到问题可以拜读,非常感谢博主的总结 读写文件是最常见的IO操作 ...

  9. python文件读写,以后就用with open语句

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

随机推荐

  1. git 格式化输出版本信息

    git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&l ...

  2. Shell命令_正则表达式

    正则表达式是包含匹配,通配符是完全匹配 基础正则表达式 test.txt示例文件 1 2 3 4 5 6 7 8 9 10 11 12 Mr. James said: he was the hones ...

  3. java-commons-HttpClient超时设置setConnectionTimeout和setSoTimeout

    问题 之前使用httpclient请求数据 源码方法: public static String doHttp(HttpMethod result, int timeout, String chars ...

  4. 绑定: TemplateBinding 绑定, 与 RelativeSource 绑定, 与 StaticResource 绑定

    介绍背水一战 Windows 10 之 绑定 TemplateBinding 绑定 与 RelativeSource 绑定 与 StaticResource 绑定 示例1.演示 TemplateBin ...

  5. Linux下的删除命令

    Linux:rm Windows:del rm parameter: -f, --force    忽略不存在的文件,从不给出提示.-i, --interactive 进行交互式删除-r, -R, - ...

  6. struts2 CVE-2013-1965 S2-012 Showcase app vulnerability allows remote command execution

    catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...

  7. ubuntu 设置 NAT 转发

    针对需求: 嵌入式开发中,经常使用板子和笔记本通过网线直连,如果需要板子连接到外网,就比较尴尬. 最简单方法,可以把板子接到局域网内,我们的笔记本通过局域网交换机连接到板子,可是,这样要很多修改 IP ...

  8. squid节点添加新域名测试

    squid节点添加新域名 测试是否缓存成功 #!/bin/bash #-- clear #清屏 方便输出结果观看 url=* #需要测试的url array_node[]="*" ...

  9. iOS应用第三方推送的添加

    现在的一些第三方的推送平台挺好用,主要是因为他们有类似微信公众平台一样的管理后台,简单易用,封装了很多开发者需要的推送功能. 下面以个推为例: 1.在个推的应用配置iOS部分设置自己的BounleID ...

  10. GoJS使用

    1. 先学习官网的这篇Get Started with GoJS教程入门,了解GoJS基础知识: 2. 浏览GoJS各个示例(Samples.Intro),找到契合自己业务需要的某个或者某几个例子,然 ...