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 实现简单的 Web
简单的学了下Python, 然后用Python实现简单的Web. 因为正在学习计算机网络,所以通过编程来加强自己对于Http协议和Web服务器的理解,也理解下如何实现Web服务请求.响应.错误处理以及 ...
- 用 python实现简单EXCEL数据统计
任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...
- python开启简单webserver
python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...
- Python开发简单爬虫 - 慕课网
课程链接:Python开发简单爬虫 环境搭建: Eclipse+PyDev配置搭建Python开发环境 Python入门基础教程 用Eclipse编写Python程序 课程目录 第1章 课程介绍 ...
- python使用简单http协议来传送文件
python使用简单http协议来传送文件!在ubuntu环境下,局域网内可以使用nc来传送文件,也可以使用基于Http协议的方式来下载文件我们可以使用python -m SimpleHTTPServ ...
- Python超简单的HTTP服务器
Python超简单的HTTP服务器 安装了python就可以 python -m SimpleHTTPServer 执行这一个命令即可实现一个HTTP服务器,将当前目录设为HTTP服务目录,可以通过h ...
- 教学项目之-通过Python实现简单的计算器
教学项目之-通过Python实现简单的计算器 计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...
- python多线程简单例子
python多线程简单例子 作者:vpoet mail:vpoet_sir@163.com import thread def childthread(threadid): print "I ...
- python实现简单的循环购物车小功能
python实现简单的循环购物车小功能 # -*- coding: utf-8 -*- __author__ = 'hujianli' shopping = [ ("iphone6s&quo ...
随机推荐
- 手动实现staticmethod和classmethod装饰器
首先,staticmethod和classmethod装饰器是通过非数据描述符实现的.用法简单,这里就不细说了. 这里主要分析一下staticmethod和classmethod是如何通过描述符实现的 ...
- 利用快排partition求前N小的元素
求前k小的数,一般人的想法就是先排序,然后再遍历,但是题目只是求前N小,没有必要完全排序,所以可以想到部分排序,而能够部分排序的排序算法我能想到的就是堆排序和快排了. 第一种思路,局部堆排序. 首先, ...
- A1012. The Best Rank
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- sscanf,sprintf 通过字符串与其它类型进行方便快捷的输入和输出
http://c.biancheng.net/cpp/html/296.html 头文件:#include <stdio.h> sscanf()函数用于从字符串中读取指定格式的数据,其原型 ...
- MyEclipse新建工作空间后的配置详细步骤
1. General --> Workspace --> UTF-8 2. General --> Editors -->File Associations --> J ...
- theano使用
一 theano内置数据类型 只有thenao.shared()类型才有get_value()成员函数(返回numpy.ndarray)? 1. 惯常处理 x = T.matrix('x') # t ...
- 信号处理——EMD、VMD的一点小思考
作者:桂. 时间:2017-03-06 20:57:22 链接:http://www.cnblogs.com/xingshansi/p/6511916.html 前言 本文为Hilbert变换一篇的 ...
- springcloud的配置文件的读取顺序
SpringBoot默认支持properties和YAML两种格式的配置文件.前者格式简单,但是只支持键值对.如果需要表达列表,最好使用YAML格式.SpringBoot支持自动加载约定名称的配置文件 ...
- Makefile ------ := ?= += =的区别
Makefile 中:= ?= += =的区别 新建一个Makefile,内容为: ifdef DEFINE_VRE VRE = “Hello World!” endif ifeq ($(OPT),d ...
- 自动检测ie低版本,并显示升级浏览器的自定义页面,当用f12再把浏览器版本提高的时候,又会自动显示正常的页面。
代码: <!--[if lte IE 9 ]> <div class=""> //这里面可以添加自己定义的内容 </div> <scrip ...