登陆流程图:



代码实现:

#-*- 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. GNOME Shell Extension常用扩展

    这篇博文的,主要目的是为了方便我和大家安装GNOME扩展.我将我安装过的所有扩展列在此处. 常用扩展 Clipboard Indicator https://extensions.gnome.org/ ...

  2. 2019.3.18考试&2019.3.19考试&2019.3.21考试

    2019.3.18 C O D E T1 树上直接贪心,环上for一遍贪心 哇说的简单,码了将近一下午终于码出来了 感觉自己码力/写题策略太糟糕了,先是搞了一个细节太多的写法最后不得不弃疗了,然后第二 ...

  3. 【洛谷P1248】加工生产调度

    题目大意:某工厂收到了n个产品的订单,这n个产品分别在A.B两个车间加工,并且必须先在A车间加工后才可以到B车间加工.某个产品i在A.B两车间加工的时间分别为Ai.Bi.怎样安排这n个产品的加工顺序, ...

  4. typescript函数(笔记非干货)

    函数类型 Function Type 为函数定义类型 Define types for functions 我们可以给每个参数添加类型之后再为函数本身添加返回值类型. TypeScript能够根据返回 ...

  5. ReactNative快速入门

    首先放图 这就是我通过简单的搭建环境写出的helloworld和使用的button组件. 那么搭建环境如何搭建呢? 使用的软件有:Node 最新版,Python2.7,Android环境要有配置And ...

  6. 多线程(Thread,Runnable)

    一.多线程. 1.进程:一个正在执行的程序叫做进程. 每一个进程的执行都有一个执行顺序,这个顺序就是一个执行的路径,或者叫做一个控制单元. 2.线程:就是上述进程中的一个独立控制单元, 线程在控制着进 ...

  7. 记录一次nginx的upstream的配置信息

    nginx的upstream的配置信息 upstream qq.xiaoyu.cn { server 192.168.1.139:80 max_fails=3 fail_timeout=30s; se ...

  8. JVM总结(六):晚期(运行期)优化

    这节我们总结一下JVM运行期的优化问题. JVM运行期优化 即时编译器(JIT) 编译对象与触发条件 编译对象 触发条件 编译过程 编译优化技术 JVM运行期优化 Java程序在运行的期间,可能会有某 ...

  9. C# 窗体最大化(自适应任务栏位置)

    this.FormBorderStyle = FormBorderStyle.None; this.Location=Screen.PrimaryScreen.WorkingArea.Location ...

  10. IO流总结笔记三

    ​ 字节流: 抽象基类:InputStream, OutputStream. 字节流可以操作任何数据.注意:字符流使用的数组是字符数组.Char [] chs 字节流使用的数组是字节数组.Byte [ ...