用Python实现用户登录接口
2014-10-06 18:44
337 查看
项目需求描述:要求用户输入用户名和密码,认证成功后显示欢迎信息,如果连续输错三次密码则锁定该用户。
逻辑流程图:
实现代码:
逻辑流程图:
实现代码:
#!/usr/bin/env python import sys account_file = 'account.txt' lock_file = 'lock.txt' # put accounts in a list fh_account = open(account_file) account_list = fh_account.readlines() fh_account.close() # every account has 3 chances of attempt at the beginning retry_count = {} for line in account_list: line = line.split() retry_count[line[0]] = 3 while True: # put locked accounts in a list fh_lock = open(lock_file) lock_list = [] for i in fh_lock.readlines(): line = i.strip('\n') lock_list.append(line) fh_lock.close() # handle the username and password empty issue username = raw_input('Username: ').strip() if len(username) == 0: print '\033[31;1mUsername should not be empty !\033[0m' continue password = raw_input('Password: ').strip() if len(password) == 0: print '\033[31;1mPassword should not be empty !\033[0m' continue # authentication part if username in lock_list: print "\033[31;1mSorry, '%s' is locked already !\033[0m" % username continue if not retry_count.has_key(username): # inexistent account retry_count[username] = 3 for line in account_list: line = line.split() if username == line[0] and password == line[1]: # authentication pass retry_count[username] = 3 # reset retry times for this account sys.exit('\033[32;1mWelcome %s login my system !\033[0m' % username) else: # authentication failed print '\033[31;1mWrong username or password !\033[0m' retry_count[username] -= 1 if retry_count[username] > 0: print '\033[31;1mYou have %d more chances !\033[0m' % retry_count[username] else: # no more chance of attempt fh = open(lock_file, 'a') fh.write('%s\n' % username) fh.close() print "\033[31;1mSorry, '%s' is locked !\033[0m" % username
相关文章推荐
- Python的Flask框架中实现登录用户的个人资料和头像的教程
- python实现用户登录系统
- Python实现登录接口的示例代码
- Python练习,模拟用户登录接口
- Python基础练习之用户登录实现代码分享
- 用Python实现web端用户登录和注册功能的教程
- Python不使用函数,实现用户登录控制
- Python 学习练习之用户登录接口
- python实现用户登录限制
- 用python连接数据库,实现用户注册和用户登录
- Python实现简单的用户登录
- 学习python——python实现简单的用户登录
- 练习:python 操作Mysql 实现登录验证 用户权限管理
- python3.6 下连接mysql数据库实现用户登录与注册
- python实现用户登录功能模块
- python学习系列之python装饰器基础(3)---装饰器实现用户登录验证
- python简单实现用户表单登录
- 集合框架_用户登录注册案例详细分析和分包的实现、用户基本描述类和用户操作接口的实现、用户操作类的具体实现、用户测试类的实现、加入猜数字小游戏
- php实现简单的用户注册,登录,修改个人信息接口
- python 3.0 模拟用户登录功能并实现三次错误锁定