登陆流程图:



代码实现:

#-*- 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. sscanf,sprintf 通过字符串与其它类型进行方便快捷的输入和输出

    http://c.biancheng.net/cpp/html/296.html 头文件:#include <stdio.h> sscanf()函数用于从字符串中读取指定格式的数据,其原型 ...

  2. Linux:从文件中搜索关键字并显示行数(cat,grep函数)

    假如有test1.txt的格式如下图所示: 有test2.txt的内容如下: 现需将test2.txt含有的关键字的行搜索出来并显示行数 则可以用到命令: cat test1.txt | grep - ...

  3. echarts 取消图例上的点击事件和图表上鼠标滑过点击事件

    备注:标黄的代码是起作用的代码,其他的不需要借鉴!!! //取消 鼠标滑过的提示框 tooltip : {         trigger: 'item',      show:false,      ...

  4. idea tomcat上传图片,无法显示的问题解决

    真是浪费时间啊,之前eclipse下的tomcat 去webapps下找就行了,这倒好,idea下根本没有. 我上传的目录建在项目中的static中了,upload文件夹,上传到这里肯定是找不到了的: ...

  5. 教你如何用Meterpreter渗透Win系统

    在这篇文章中,我们将跟大家介绍如何使用Meterpreter来收集目标Windows系统中的信息,获取用户凭证,创建我们自己的账号,启用远程桌面,进行屏幕截图,以及获取用户键盘记录等等. 相关Payl ...

  6. Exception in thread "main" java.util.InputMismatchException

    今天写代码来了一个异常 /** * 需求分析:根据输入的天数是否是周六或是周日, * 并且天气的温度大于28摄氏度,则外出游泳,否则钓鱼 * @author chenyanlong * 日期:2017 ...

  7. conda常用命令总结

    conda 一些背景历史以及如何安装这里就不说了,因为实在是漫天都在飞,随便都能找到相关的资料.我这里只是将平时常用到的 Conda 命令进行汇总,以备不时之需,因为我也是一个忘性极大的人,实在是记不 ...

  8. eclipse中编辑properties文件无法看到中文

    如果在eclipse中编辑properties文件无法看到中文则参考“Eclipse开发环境配置-indigo.docx”添加propedit插件.

  9. 即将上线的Kafka服务器面临的一系列填坑笔记

      即将上线的Kafka服务器面临的一系列填坑笔记 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Kafka日志报错:[error] k.m.j.KafkaJMX$ - Fai ...

  10. nGrinder TestGroovy.groovy

    s /** * */ package com.iteye.lindows.mysql /** * @author Lindows * */ class TestGroovy { static main ...