#!/usr/bin/env python
#-*- encoding: utf- -*-
import sys
import os
import getpass
import platform
# 全局变量
retry_limit = # 登录重试次数
retry_count = # 计数器
account_file = 'account.txt'
lock_file = 'account_lock.txt'
def lock(user):
lock_check = open(lock_file, 'r') # 打开account_lock.txt,检查用户是否已经存在文件中
for line in lock_check.readlines(): # 循环读取文件内容
if user == line.strip('\n'): # 去掉指定字符,并且判断是用户名是否和文件中的文件名一致
sys.exit('\033[35mUser %s is locked!!!\033[0m' % user) # 存在则直接退出并打印输出提示
lock_check.close() # 关闭打开的文件流
def login(user, passwd):
global retry_count # 函数内要改变全局变量的值 "retry_count += 1",需要加上global 关键字
file = open(account_file, 'r')
match_flag = False
for line in file.readlines():
username, password = line.strip('\n').split()
if user == username and passwd == password:
print 'hello, %s !!' % username
match_flag = True
break
file.close()
if match_flag == False:
print 'sorry,%s is unmatched' % username
retry_count +=
else:
print 'wlecome login my learning system!'
sys.exit()
def main():
# 清屏操作,判断当前运行环境在什么平台下
if platform.system() != 'Windows':
os.system('clear')
else:
os.system('cls')
while retry_count < retry_limit:
username = raw_input('username: ')
lock(username)
password = getpass.getpass('password: ') # 用getpass模块中的getpass方法来实现输入密码时和Linux一样
login(username, password)
else:
print "you account %s is locked!!!" % username
g = open(lock_file,'a')
g.write(username)
g.write('\n')
g.close()
if __name__ == '__main__':
main()