python实现简单登陆流程的方法

作者:郑子明 时间:2021-01-30 00:48:24 

登陆流程图:

python实现简单登陆流程的方法

代码实现:


#-*- coding=utf-8 -*-
import os,sys,getpass
'''
user.txt 格式
账号 密码 是否锁定 错误次数
jack 123 unlock 0
tom 123 unlock 0
lily 123 unlock 0
hanmeimei 123 unlock 0
lucy 123 unlock 0
'''
# 定义写入文件的函数
def wirte_to_user_file(users,user_file_path):
user_file = file(user_file_path,'w+')
for k,v in users.items():
line = []
line.append(k)
line.extend(v)
user_file.write(' '.join(line)+'\n')
user_file.close()
# 判断用户文件是否存在,不存在直接退出
user_file_path = 'users.txt'
if os.path.exists(user_file_path):
user_file = file(user_file_path,'r')
else:
print 'user file is not exists'
sys.exit(1)
# 遍历用户文件,将用户包装成字典
users_dic = {}
for user_line in user_file:
user = user_line.strip().split()
users_dic[user[0]] = user[1:]
'''
{
'lucy': ['123', 'unlock', '0'],
'lily': ['123', 'unlock', '0'],
'jack': ['123', 'unlock', '0'],
'hanmeimei': ['123', 'unlock', '0'],
'tom': ['123', 'unlock', '0']
}
'''
while True:
# 输入账号
input_name = raw_input('please input your username,input "quit" or "q" will be exit : ').strip()
# 判断是否为退出
if input_name == 'quit' or input_name == 'q':
sys.exit(0)
# 输入密码
password = getpass.getpass('please input your password:').strip()
# 判断账号是否存在、是否锁定
if input_name not in users_dic:
print 'username or password is not right'
break

if users_dic[input_name][1] == 'lock':
print 'user has been locked'
break

# 判断密码是否正确,正确,登陆成功
if str(password) == users_dic[input_name][0]:
print 'login success,welcome to study system'
sys.exit(0)
else:
# 如果密码错误则修改密码错误次数
users_dic[input_name][2] = str(int(users_dic[input_name][2])+1)
# 密码错误次数大于3的时候则锁定,并修改状态

if int(users_dic[input_name][2]) >= 3:
print 'password input wrong has 3 times,user will be locked,please connect administrator'
users_dic[input_name][1] = 'lock'
wirte_to_user_file(users_dic,user_file_path)
break

wirte_to_user_file(users_dic,user_file_path)

来源:https://blog.csdn.net/reblue520/article/details/55510225

标签:python,登陆
0
投稿

猜你喜欢

  • pycharm安装和首次使用教程

    2022-05-23 19:01:36
  • python游戏测试工具自动化遍历游戏中所有关卡

    2021-10-05 13:03:20
  • Debugging JavaScript:throw与console

    2008-08-29 17:03:00
  • 使用Python爬取最好大学网大学排名

    2023-09-17 09:24:45
  • python版飞机大战代码分享

    2023-11-13 22:29:03
  • JS实现的倒计时效果实例(2则实例)

    2023-08-23 17:12:05
  • Python 获取主机ip与hostname的方法

    2021-05-13 09:30:30
  • MYSQL教程:数据列类型与查询效率

    2009-02-27 15:37:00
  • python3+PyQt5实现柱状图

    2023-06-02 22:19:36
  • 实例演示在SQL中启用全文检索

    2011-10-01 14:01:37
  • python批量读取txt文件为DataFrame的方法

    2021-09-29 12:04:21
  • em与px的区别以及em特点和应用

    2008-11-11 12:03:00
  • Python解析pcap文件示例

    2023-05-16 00:08:45
  • python访问sqlserver示例

    2022-07-15 07:25:01
  • 解析Python 偏函数用法全方位实现

    2023-12-22 00:06:03
  • django-crontab实现服务端的定时任务的示例代码

    2021-02-13 05:15:06
  • python爬取豆瓣电影排行榜(requests)的示例代码

    2022-10-16 02:18:46
  • python 合并列表的八种方法

    2022-06-10 10:07:29
  • pytorch: tensor类型的构建与相互转换实例

    2023-06-14 09:22:57
  • python3下载抖音视频的完整代码

    2023-12-24 01:10:42
  • asp之家 网络编程 m.aspxhome.com