扣丁学堂Python培训简述Python实现简单登陆流程的方法

2018-06-12 14:18:59 585浏览

本篇文章小编主要和大家分享一下扣丁学堂Python培训简述的Python实现简单登陆流程的方法,文章中会有代码列出供大家参考学习,对Python开发感兴趣的小伙伴就随小编一起来了解一下吧。


扣丁学堂Python培训简述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)


以上就是扣丁学堂Python在线学习小编给大家分享的Python实现简单登陆流程的方法,希望对小伙伴们有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。扣丁学堂是专业的Python培训机构,不仅有专业的老师和与时俱进的课程体系,还有大量的Python在线教程供学员挂看学习哦。扣丁学堂Python技术交流群:279521237。


扣丁学堂微信公众号

关注微信公众号获取更多学习资料



查看更多关于"Python开发资讯"的相关文章>




标签: 扣丁学堂Python培训 Python实现简单登陆流程的方法 Python培训 Python视频教程 Python基础教程 python安装教程 Python核心编程 Python在线教程 Python在线视频 Python在线学习

热门专区

暂无热门资讯

课程推荐

微信
微博
15311698296

全国免费咨询热线

邮箱:codingke@1000phone.com

官方群:148715490

北京千锋互联科技有限公司版权所有   北京市海淀区宝盛北里西区28号中关村智诚科创大厦4层
京ICP备12003911号-6   Copyright © 2013 - 2019

京公网安备 11010802030908号