登陆流程图:



代码实现:

#-*- 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实现简单登陆流程的更多相关文章

  1. Python 实现简单的 Web

    简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...

  2. 用 python实现简单EXCEL数据统计

    任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...

  3. python开启简单webserver

    python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...

  4. Python开发简单爬虫 - 慕课网

    课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序   课程目录 第1章 课程介绍 ...

  5. python使用简单http协议来传送文件

    python使用简单http协议来传送文件!在ubuntu环境下,局域网内可以使用nc来传送文件,也可以使用基于Http协议的方式来下载文件我们可以使用python -m SimpleHTTPServ ...

  6. Python超简单的HTTP服务器

    Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...

  7. 教学项目之-通过Python实现简单的计算器

    教学项目之-通过Python实现简单的计算器   计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...

  8. python多线程简单例子

    python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...

  9. python实现简单的循环购物车小功能

    python实现简单的循环购物车小功能 # -*- coding: utf-8 -*- __author__ = 'hujianli' shopping = [ ("iphone6s&quo ...

随机推荐

  1. FFMEPG -- A ffmpeg and SDL Tutorial : tutorial05

    修改了同步播放ffmpeg问题.并且增加可以放大视频. // tutorial05.c // A pedagogical video player that really works! // // C ...

  2. Linux下定时器

    http://unix8.net/linux%E4%B8%8B%E5%AE%9A%E6%97%B6%E5%99%A8.html 一. 基础知识 1.时间类型.Linux下常用的时间类型有4个:time ...

  3. Raspbian首次安装后无法使用SSH链接

    使用Putty连接树莓派,出现Network Error:Connection Refused 新版的Raspbian系统默认禁用了SSH. 解决方法:在/boot分区创建名为"ssh&qu ...

  4. [luogu4462][异或序列]

    传送门 突然发现自己没整理过异或的知识,正好借这个题整理一下. 关于异或 (1)异或就是在二进制下,两数各个位置上的数,相同为0,不同为1,所得到的数,比如说4^7,4的二进制是100,7的二进制是1 ...

  5. MSSQL获取当前插入的ID号及在高并发的时候处理方式

    SQL Server 2000中,insert数据的时候返回自动编号的id,有三种方法实现SCOPE_IDENTITY.IDENT_CURRENT 和 @@IDENTITY,它们都返回插入到 IDEN ...

  6. idea 显示properties 中文

    有时我们打开.properties文件时,中文显示为utf8编码格式,可以在file->setting->editor->file encodings下 把transparent n ...

  7. gcc/g++

    $gcc -g -Wall -ansi -pedantic main.cpp -lstdc++ -std=c++11 -lpthread -o xmain

  8. promise第一篇-简介

    1. 创建一个promise对象 var promise = new Promise(function(resolve, reject){ //异步处理 //处理结束后调用resolve或reject ...

  9. Spark集群之yarn提交作业优化案例

    Spark集群之yarn提交作业优化案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.启动Hadoop集群 1>.自定义批量管理脚本 [yinzhengjie@s101 ...

  10. SQL2008.sa'登录失败(错误18456)

    其实不仅仅是2008,高版本的也有这个问题.网上一大堆解决这个问题的方法,无非就是启动这个,启用那个.这里我讲些不一样的. 当你开启了TCP协议之后,需要去Windows防火墙哪里写一下入站规则,如图 ...