Python实现登录接口的示例代码
作者:Dreamer_qiao 时间:2022-08-28 19:50:31
之前写了Python实现登录接口的示例代码,最近需要回顾,就顺便发到随笔上了
要求:
1.输入用户名和密码
2.认证成功,显示欢迎信息
3.用户名3次输入错误后,退出程序
4.密码3次输入错误后,锁定用户名
Readme:
1.UserList.txt 是存放用户名和密码的文件,格式为:username: password,每行存放一条用户信息
2.LockList.txt 是存放已被锁定用户名的文件,默认为空
3.用户输入用户名,程序首先查询锁定名单 LockList.txt,如果用户名在里面,提示用户被锁定,并退出程序
4.如果用户名不在锁定名单里,程序将查询用户名单 UserList.txt ,如果用户名不在里面,将提示用户不存在,请重新输入,三次输入错误,将退出程序
5.如果用户名在用户名单里,将提示用户输入密码,密码正确,显示欢迎信息;3次输入错误,将锁定此用户名(写入锁定名单)
流程图:
代码:
# Joe Young
import os, sys, getpass
os.system('cls') #调用os模块的system方法传入'cls'参数,清屏
count = 0 #用户名登录次数计数
while count < 3:
username = input('username:')
lock_file = open('LockList.txt', 'r+') #打开LockList.txt文件,权限r+(打开用于读和写文件。文件指针置于该文件的开头)
lock_list = lock_file.readlines() #使用readlines()方法逐行读取LockList.txt,生成列表,并赋值给lock_list
for lock_line in lock_list:
if username == lock_line.strip('\n'): #使用strip()方法去掉换行符,判断username是否在LockList.txt
print('用户名 %s 已被锁定,请联系管理员...' %(username))
sys.exit(1) #sys模块的exit()方法表示退出
with open('UserList.txt', 'r') as user_file: #打开UserList.txt,权限只读
user_list = user_file.readlines() #逐行读取UserList.txt文件,赋值给user_list变量
for user_line in user_list:
(user, passwd) = user_line.strip('\n').split(': ') #获取user,passwd的值,用split(': ')实现分割字符串
if user == username: #判断用户名是否在UserList.txt文件内
n = 0 #密码输入次数计数
while n < 3: #3次输入机会
password = getpass.getpass('password:') #使用getpass模块的getpass()方法获取用户输入的密码
if password == passwd: #判断密码是否匹配
print('欢迎 %s 登陆系统!' %(username))
sys.exit(0)
else:
if n != 2: #n=2时,是最后一次机会,不需要提示还剩下0次机会
print('密码错误,请重新输入,您还有 %d 次机会\n' %(2-n))
n += 1 #密码输入错误,次数+1
else:
lock_file.write(username + '\n') #密码输入错误次数达到3次,把用户名写入LockList.txt文件,锁定用户名
sys.exit('错误次数过多,用户名已被锁定...') #程序退出,并输出提示
else: #用户名不存在,执行else语句
if count != 2: #count=2时,是最后一次输入用户名的机会,不用提示还剩下0次机会了
print('用户名不存在,请重试,您还有 %d 次机会\n' %(2-count))
count += 1 #用户名输入错误,count+1
else: #用户名输入错误次数达到3次
sys.exit('输入次数过多,程序已退出...') #退出程序,并输出提示
lock_file.close() #关闭LockList.txt文件
来源:http://www.cnblogs.com/Dreamer-qiao/p/7217026.html?utm_source=tuicool&utm_medium=referral
标签:Python,登录接口
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入了解python的函数参数
2023-07-24 08:19:01
![](https://img.aspxhome.com/file/2023/2/59472_0s.png)
Pycharm无法显示动态图片的解决方法
2023-01-29 23:13:42
![](https://img.aspxhome.com/file/2023/0/89420_0s.jpg)
详解python编译器和解释器的区别
2023-06-26 03:53:09
巧用SQL链接服务器访问远程Access数据库
2008-11-28 16:32:00
python使用ddt过程中遇到的问题及解决方案【推荐】
2021-08-25 09:20:44
![](https://img.aspxhome.com/file/2023/7/112967_0s.jpg)
PyQt5-QDateEdit的简单使用操作
2022-09-16 17:35:51
python实现学生管理系统源码
2023-05-29 22:05:16
![](https://img.aspxhome.com/file/2023/9/62919_0s.jpg)
简单了解什么是神经网络
2023-10-11 22:26:34
![](https://img.aspxhome.com/file/2023/8/89318_0s.png)
使用面向对象的技术创建高级JavaScript Web 应用程序
2008-11-03 13:00:00
![](https://img.aspxhome.com/file/UploadPic/200811/3/code-21s.gif)
详解Python爬虫的基本写法
2022-12-26 07:49:45
Python处理Excel文件实例代码
2022-02-15 23:13:01
python实现DNS正向查询、反向查询的例子
2022-10-29 07:18:55
Python线程之如何解决共享变量问题
2023-08-27 16:15:56
![](https://img.aspxhome.com/file/2023/0/70700_0s.png)
火车票抢票python代码公开揭秘!
2021-06-16 18:34:29
![](https://img.aspxhome.com/file/2023/1/93571_0s.jpg)
忘记ftp密码使用python ftplib库暴力破解密码的方法示例
2021-01-02 03:08:31
用python画了个圣诞树给女朋友
2022-06-03 00:54:44
![](https://img.aspxhome.com/file/2023/5/97355_0s.jpg)
Django限制API访问频率常用方法解析
2022-06-24 18:20:13
Pycharm设置界面全黑的方法
2021-09-15 11:13:51
python编写脚本之pyautogui的安装和使用教程
2021-06-17 09:48:11
![](https://img.aspxhome.com/file/2023/8/92498_0s.png)
自然语言处理NLP TextRNN实现情感分类
2022-01-20 11:14:47