博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python实现简单登陆流程
阅读量:6894 次
发布时间:2019-06-27

本文共 1859 字,大约阅读时间需要 6 分钟。

登陆流程图:

代码实现:

#-*- coding=utf-8 -*-import os,sys,getpass'''user.txt 格式账号 密码 是否锁定 错误次数jack 123 unlock 0tom 123 unlock 0lily 123 unlock 0hanmeimei 123 unlock 0lucy 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://www.cnblogs.com/reblue520/p/6555913.html

你可能感兴趣的文章
KOTree
查看>>
BlockAlertsAnd-ActionSheets
查看>>
开源 java CMS - FreeCMS2.5 标签formTable自定义表单
查看>>
FreeCMS视频教程 将FreeCMS导入myeclipse
查看>>
Android 8.0 SystemUI(一):图文并茂的介绍 :D
查看>>
1wifi 简介(框架)
查看>>
internet && intranet
查看>>
go get报错 error: RPC failed; result=56, HTTP code =
查看>>
串行(Sequential)、并发(Concurrent)、并行(parallel)与分布式
查看>>
JAVA NIO学习笔记之Channel(基础篇)
查看>>
Xcode升级到6.4之后插件无法使用,重新安装最新也无法使用的解决办法
查看>>
秒懂科技新概念
查看>>
eclipse启动tomcat无法访问
查看>>
Notepad++ 书签
查看>>
TiDB 集群测试
查看>>
十天学会php之第五天
查看>>
Java基础10
查看>>
jquery基础学习二
查看>>
为什么说写“安装指南”类博客的程序员是懒惰的
查看>>
Android模拟器——Genymotion(很快)
查看>>