1 #-*- encoding: utf-8 -*- 2 3 class DBMnagement(object): 4 5 '数据库管理类' 6 7 def __init__(self, nm, pwd): 8 9 self.name = nm 10 11 self.password = pwd 12 13 def updatePWD(self, newpwd): 14 15 self.password = newpwd 16 17 def __del__(self): 18 19 pass 20 21 #import os 22 23 import time 24 25 def CreateLog(username): 26 27 '登陆成功写时间戳到log文件' 28 29 timeStamp = time.ctime(time.time()) 30 31 log = 'User' + ' - ' + username + ', last time login Succeed at ' + timeStamp + '\n' 32 33 logFile = open('d:\\EX13_4\\userlog.txt', 'a') 34 35 logFile.write(log) 36 37 logFile.close() 38 39 def ReadLog(): 40 41 '登陆成功,从log文件读取上次时间戳' 42 43 logFile = open('d:\\EX13_4\\userlog.txt') 44 45 logInfo = logFile.readlines() 46 47 if len(logInfo) >= 1: 48 49 print logInfo[-1] 50 51 logFile.close() 52 53 def ChangePWDinLog(username, newpwd): 54 55 '修改用户密码' 56 57 credentialFile = open('d:\\EX13_4\\accounts.txt', 'w') 58 59 credentialtxt = username + ' ' + newpwd 60 61 credentialFile.write(credentialtxt) 62 63 credentialFile.close() 64 65 print '密码已修改 ... Password updated successfully. \n' 66 67 # 从这里开始是主程序 68 69 print '欢迎使用最简单的用户管理系统' 70 71 print '尝试用系统中已经存在的用户名和密码登录,有三次机会' 72 73 print '登陆成功后输入 updatepwd 命令可以修改密码,成功后退出系统' 74 75 print '登陆成功后输入 quit 命令退出系统\n' 76 77 credentialFile = open('d:\\EX13_4\\accounts.txt') # 从文本文件中获取用户名和密码 78 79 credentialInfo = credentialFile.readlines() 80 81 credentialFile.close() 82 83 usernameinDB = credentialInfo[-1].split()[0] # 这里credentialInfo[-1]是一个字符串,包括用户名和口令,空格隔开的 84 85 passwordindm = credentialInfo[-1].split()[1] 86 87 errorTimes = 3 88 89 while errorTimes: 90 91 print '请输入用户名和密码:' 92 93 input_username = raw_input('Username ... : ') 94 95 input_password = raw_input('Password ... : ') 96 97 if input_username == usernameinDB and input_password == passwordindm: 98 99 print '登陆成功 ... Login Successful' 100 101 CurrentUser = DBMnagement(input_username, input_password) 102 103 ReadLog() 104 105 CreateLog(input_username) 106 107 input_command = raw_input('请输入你的命令, quit or updatepwd ... \n') 108 109 if input_command == 'quit': 110 111 del CurrentUser 112 113 print '\n已退出系统 ... Logout successfully.' 114 115 break 116 117 elif input_command == 'updatepwd': 118 119 newpwd = raw_input('\n请输入新密码, new password ... : ') 120 121 CurrentUser.updatePWD(newpwd) 122 123 ChangePWDinLog(CurrentUser.name, CurrentUser.password) 124 125 del CurrentUser 126 127 print '\n已退出系统 ... Logout successfully.' 128 129 break 130 131 else: 132 133 print '错误命令 ... Wrong Command' 134 135 del CurrentUser 136 137 print '\n已退出系统 ... Logout successfully.' 138 139 break 140 141 else: 142 143 print '用户名或口令错误 ... Wrong Username or Password.\n' 144 145 errorTimes -= 1 146 147 if errorTimes == 0: print '已退出系统 ... Logout successfully.'
欢迎点击http://www.cnblogs.com/X-zone/梦猩魂的个人博客