【Python之旅】第一篇:基于文件处理的登陆接口
1.基本需求
编写登陆接口,实现如下需求:
(1)输入用户名密码
(2)认证成功后显示欢迎信息
(3)输错三次后锁定
2.实现细节
·每添加一个用户,需要手动添加三个文件
文件 | 功能 |
username_count.txt | 记录用户输错密码的次数,最大为3次,如果用户密码输入正确,则重置为0,默认为0 |
username_lock.txt | 记录用户是否被锁定,1表示锁定,0表示未锁定,默认为0 |
username_passwd.txt | 记录用户的密码 |
·注:username是指该用户的用户名,视具体的用户名而定;
·每添加一个用户,在username.txt中写入该用户名,一个用户名占一行(手动);
·如果输入的用户名或密码为空,都要求重新输入;
·输入不存在的用户名,只会提示“username or password wrong”;
·输入存在的用户名,才会进行密码输入错误次数的记录。
3.实现代码与注释
import sys,oscount = 0 #To count, if the user is locked. mark_user = 0 #To make sure if the user is existing. mark_passwd = 0 #To make sure if the password is right.while count < 3:name = raw_input('Username:').strip()if len(name) == 0:print 'The username can not be empty!'continuekey = raw_input('Password:').strip() if len(key) == 0:print 'The password can not be empty!Try again!'continue#Upon:To check if the username or password is empty.检查用户名或密码是否为空f = file('username.txt', 'r')userlist = f.readlines()f.close()for user in userlist:if user.strip() == name:mark_user = 1#检查输入的用户是否在username.txt文件中,即是否注册,如果在,mark_user = 1if mark_user == 1: f = file('%s_lock.txt' % (name), 'r')locked = int(f.readline().strip())f.close() else:print 'Username or password wrong!' break #Upon:To check if exist,if not exist,printing username or password wrong!And quit the program. #用户名不存在就提示用户名或密码错误,用户名存在就检查该用户是否被锁定if locked == 1:sys.exit('Sorry, the username had been locked!!!Please call the system administrator.')#Upon:To check if the username is locked by the system.else:f = file('%s_passwd.txt' % (name), 'r')passwd = (f.readline().strip())if passwd.strip() == key:mark_passwd = 1# check the password from the name_input password file.if mark_user == 1 and mark_passwd == 1:f = file('%s_count.txt' % name, 'w')f.write('0')f.close()sys.exit('%s, welcome to our system!' % name)#用户名和密码正确,将username_count.txt内容重置为0 else:f = file('%s_count.txt' % name, 'r')count = int((f.read().strip()))f.close()count += 1f = file('%s_count.txt' % name, 'w')f.write(str(count))f.close()#open the count file of the user and change it. #密码不正确,就把count次数记录在该用户名对应的文件count文件上print '''Username or password wrong! And the username '%s' has %d more chances to retry!''' % (name, 3 - count)if count == 3:print "'%s' has been locked!!!" % (name)if os.path.exists('%s_lock.txt' % (name)):fobj = open('%s_lock.txt' % (name), 'w')fobj.writelines('1\n')else:print 'Username or password wrong!'continue#Upon:To check if the username and password are right, if not, the count will add 1. #如果count次数为3,则将用户对应的lock文件内容重置为1,锁定该用户
4.测试
--对单一用户xpleaf的测试
·在对应4个文件中添加相应信息:
xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt xpleaf_count.txt xpleaf_lock.txt xpleaf_passwd.txt ==> username.txt <== xpleaf==> xpleaf_count.txt <== 0 ==> xpleaf_lock.txt <== 0==> xpleaf_passwd.txt <== xpleaf123
·正常输入用户名和密码:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:xpleaf Password:xpleaf123 xpleaf, welcome to our system!
·故意输错两次密码后查看相关文件:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:xpleaf Password:klkdf Username or password wrong! And the username 'xpleaf' has 2 more chances to retry! Username:xpleaf Password:kldf Username or password wrong! And the username 'xpleaf' has 1 more chances to retry! Username:Traceback (most recent call last):File "newlogin.py", line 8, in <module>name = raw_input('Username:').strip() EOFError xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt 2
·可以看到count文件已经记录为2,再输入正确的用户名和密码:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:xpleaf Password:xpleaf123 xpleaf, welcome to our system! xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt 0
·count文件重置为0;
·连续输入密码错误超过3次:
0xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:xpleaf Password:kldf Username or password wrong! And the username 'xpleaf' has 2 more chances to retry! Username:xpleaf Password:klkdf Username or password wrong! And the username 'xpleaf' has 1 more chances to retry! Username:xpleaf Password:kldf Username or password wrong! And the username 'xpleaf' has 0 more chances to retry! 'xpleaf' has been locked!!! xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:xpleaf Password:xpleaf123 Sorry, the username had been locked!!!Please call the system administrator. xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_lock.txt 1
·用户已经被锁定,对应lock文件内容变为1,如果需要解锁,则要手动将该文件重置为0;
·输入错误的用户名:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:klkdlf Password:klkdf Username or password wrong!
·只会提示用户名或密码错误,不会做相关文件记录;
--对两个用户xpleaf和yonghaoyip的测试
·为用户yonghaoyip添加相关文件信息:
xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt yonghaoyip_count.txt yonghaoyip_lock.txt yonghaoyip_passwd.txt ==> username.txt <== xpleaf yonghaoyip==> yonghaoyip_count.txt <== 0==> yonghaoyip_lock.txt <== 0==> yonghaoyip_passwd.txt <== yonghaoyip123
·主要测试两个用户的count文件记录:
对用户yonghaoyip操作: xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:yonghaoyip Password:klkdf Username or password wrong! And the username 'yonghaoyip' has 2 more chances to retry! Username:yonghaoyip Password:kldf Username or password wrong! And the username 'yonghaoyip' has 1 more chances to retry! Username:Traceback (most recent call last):File "newlogin.py", line 8, in <module>name = raw_input('Username:').strip() EOFError对用户xpleaf操作: xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py Username:xpleaf Password:kkjlkdf Username or password wrong! And the username 'xpleaf' has 2 more chances to retry! Username:xpleaf Password:xpleaf123 xpleaf, welcome to our system!查看两个用户对应的count文件记录: xpleaf@xpleaf-machine:~/seminar6/day1$ head yonghaoyip_count.txt xpleaf_count.txt ==> yonghaoyip_count.txt <== 2 ==> xpleaf_count.txt <== 0
·可以看到两个用户的相关文件并不会相互影响。
【Python之旅】第一篇:基于文件处理的登陆接口相关推荐
- 单片机学习:第一篇 基于Python的树莓派语音助手
title: 单片机学习:第一篇 基于Python的树莓派语音助手 tags: 树莓派,python,语音助手,百度AIP 目录 一.pyaudio录音 二.语音识别 三.与图灵机器人对话 四.语音合 ...
- Python开发【第一篇】:目录
本系列博文改编自武沛齐老师的原创博文,主要包含 Python基础.前端开发.Web框架.缓存以及队列等内容 ,用于学习记录成长!!! Python开发[第一篇]:目录 Python开发[第二篇]:初 ...
- python可视化第一篇——基于matplotlib库
下文主要内容来自课堂讲义,由本人整理. 1. 画图前准备 1.1 pip安装pandas.numpy.plt库: 1.2 中文显示问题: win系统直接添加: plt.rcParams['font.s ...
- python django开发工具_利用pyCharm编辑器创建Django项目开发环境-python开发工具第一篇...
[前置说明] 1.django环境与python对应关系: Django version Python versions 1.11 2.7, 3.4, 3.5, 3.6, 3.7 (added in ...
- 程序员持续记录开始赚钱之旅 (第一篇)
今日立贴开始记录,除却本质工作之外的赚钱门路.逐渐希望能实现财务自由.第一篇算是总结归纳吧 我实在是懒,就希望有那种睡着也能继续挣钱,或者别人在帮我挣钱的那种.接下来说的是自己做过的和将要做的. 总结 ...
- python协程详解_彻底搞懂python协程-第一篇(关键词1-4)
任何复杂的概念或系统都不是凭空出现的,我们完全可以找到它的演化历程,寻根究底终会发现,其都是在一系列并不那么复杂的简单组件上发展演化而来! by 落花僧 本文通过一系列关键概念,逐步递进理解协程. 0 ...
- SpringBoot之旅第一篇-初探
2019独角兽企业重金招聘Python工程师标准>>> 一.SpringBoot是什么? 微服务,应该是近年来最火的概念,越来越多的公司开始使用微服务架构,面试中被问到的微服务的概率 ...
- JB的Python之旅-爬虫篇--urllib和Beautiful Soup
啃面包是辛苦的,那就开始学习爬虫吧,而学习爬虫的初衷很简单,爬图爬图,这就是学习的动力~ 1.爬虫信息了解 1)爬虫的定义: 先了解,什么叫爬虫,上度娘搜了一番,解释如下: 网络爬虫(又被称为网页蜘蛛 ...
- Python之旅HTML篇
目录 详情参考 HTML 标记语言 HTML标签 HTML标签分类 注释 额外知识点以及建议 标签属性 全局属性 HTML标签元素分类 文档类型声明 文档总结构 head中的标签 base title ...
- python 学习笔记第一篇---下载网页内所有图片
第一步:打开网址,进入开发者模式,选中 Network,选择 Img 然后刷新页面,静静等待页面加载 等待页面加载完成,你可以看到这边有很多图片的资源 随便点开一个图片选择 Headers 可以看到 ...
最新文章
- C++ new和delete(C++动态分配和释放内存)
- 延迟反馈带来的样本偏差如何处理
- Android 事件分发机制
- 「Python基础知识」Python的split方法如何使用
- 计算机和绘画的论文,浅析毕沙罗的绘画风格
- Picasso源码阅读笔记三
- 7-107 通讯录排序 (20 分)
- 计算机法宝,计算机专业英语学习法宝.doc
- 项目管理——项目汇报总结
- 三阶魔方大中小魔公式_三阶魔方花样大汇总 ,带公式带图
- 苏州计算机岗前培训,不忘初心 牢记使命——苏州五院2019年新职工岗前培训圆满完成...
- 使用 spacy 进行自然语言处理(一)
- 浅谈人工智能:现状、任务、构架与统一
- 零基础小白必看----2020年最新Java学习路线图(纯干货)
- Codeforces Round #533(Div. 2) A.Salem and Sticks
- Threejs实现下雨,下雪,阴天,晴天,火焰
- H3 BPM公文管理解决方案
- wcs系统安全保护功能
- 为什么女性应该考虑从事网络安全事业?
- 目标检测~无人机视角
热门文章
- (js技巧)input文本框回车或者失去光标触发事件
- 内存管理-基础知识框架和关键结构体(一)
- Shell脚本-tr 将大写字母变为小写
- Uboot系统初始化为何要初始化堆栈?为何C语言的函数调用要用到堆栈,而汇编却不需要初始化堆栈?
- linux下数据同步、回写机制分析
- 2060. 奶牛选美
- Linux——常用文件管理命令(必会)
- 0-1背包(动态规划)
- TypeError: Fetch argument has invalid type class ‘numpy.float32‘, must be a string or Tensor
- python 嵌套list的一些小结