微型用户信息管理系统MUIMS(Micro User Information Management System)V2.0
Posted wy_0928
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微型用户信息管理系统MUIMS(Micro User Information Management System)V2.0相关的知识,希望对你有一定的参考价值。
一、系统功能全系统忽略大小写,用户密码双重加密,绝对安全
1.新用户注册
(1)判断用户名是否合法(只能由字母和数字组成,长度小于等于9)
2.老用户登陆
(1)如果输入不存在的用户名,则询问是否为新用户,是新用户就进入注册程序,不是新用户就继续登陆
(2)获取上次登陆时间,如果本次登陆距离上次登陆时间间隔不足4小时,则给出提示信息且打印上次登陆时间
3.老用户改密
4.【仅限管理员】导出所有用户信息
用户名、(新)密码、上次登陆时间
5.【仅限管理员】删除用户
删除该用户在系统中所有的数据
=============================================================================================================================
二、操作手册
运行脚本
1.新用户注册(按r,回车)
2.老用户登陆(按l,回车)
(1)输入一个已存在的用户名,不停输入密码直到密码对为止
(2)输入一个不存在的用户名,按y进入注册账号,按n继续登陆已有账号
3.老用户改密(按c,回车)
先登录账号,按y确认改密,不改密按n
4.【仅限管理员】导出所有用户信息(按v,回车)
必须登陆管理员账号,否则无法导出。
5.删除用户(仅限管理员)(按c,回车)
必须登陆管理员账号,之后确认删除选y,不删除选n,选y之后会提示输入要删除用户的用户名,输入完毕后系统内有关该用户的信息全部被删除。
=============================================================================================================================
三、磁盘文件存储格式样例(分隔符:制表符TAB)
1.register_info.txt文件:用户名 密码 注册时间或改密时间 改密标记
2.login_info.txt文件:用户名 密码 登陆时间
3.other_info.txt文件:用户名 旧密码 新密码 修改时间 类别标记
(暂时只支持改密功能)
4.admin_userInfo文件:用户名 (新)密码上次登陆时间
四、代码
#-*-coding:utf-8-*-
#!/usr/bin/env python
import string
import base64
import hashlib
import datetime
regis_fpath = '/***/register_info.txt' #注册信息
login_fpath = '/***/login_info.txt' #登陆信息
other_fpath = '/***/other_info.txt' #其他信息(暂时只改密)
admin_userInfo_fpath = '/***/admin_userInfo.txt' #(管理员)批量导出用户信息
#密码双重加密,无法破解
def encodePsw(psw):
m = hashlib.md5()
psw_encode = base64.b64encode(psw)
m.update(psw_encode)
return m.hexdigest()
#判断注册的用户名是否合法:(1)只能由字母和数字组成,(2)长度小于等于9
def checkUser(user_name):
alphas = string.letters[:26] + string.digits
len0 = len(user_name)
if len0 > 9:
re = False
else:
for i in range(0,len0):
if user_name[i] in alphas:
re = True
else:
re = False
break
return re
#新用户注册(已存在的用户名不可注册)
def register():
while True:
print 'Please register!'
user_name = raw_input('Register username: ').strip().lower()
f_r = open(regis_fpath, 'r')
re1 = True
re2 = True
if checkUser(user_name) == False:
re1 =False
for eachLine in f_r:
if eachLine.startswith(user_name):
re2 = False
f_r.close()
break
if re1 and re2:
user_psw = encodePsw(raw_input('Register password: '))
regis_time = str(datetime.datetime.now())[:19]
f_r = open(regis_fpath, 'a+')
str0 = user_name + '\\t' + user_psw + '\\t' + regis_time + '\\t' + 'changePassword=False' + '\\n'
f_r.write(str0)
f_r.close()
print 'Register finished!'
break
else:
print '[Warning] Illegal username or this username has exists! Please try another!'
#获取系统中已注册的所有用户名(基于register_info.txt文件,返回由所有用户名组成的列表)
def getAllName():
f_g = open(regis_fpath, 'r')
all_name = []
for eachLine in f_g:
for i in range(0,len(eachLine)):
if eachLine[i] == '\\t':
all_name.append(eachLine[:i])
break
f_g.close()
return all_name
#获取系统中已注册用户的所有密码(基于register_info.txt文件,返回由所有密码组成的列表)
def getAllPsw():
f_g = open(regis_fpath, 'r')
all_psw = []
for eachLine in f_g:
for i in range(0,len(eachLine)):
start = eachLine.find('\\t')
end = eachLine.find('\\t',9)
break
all_psw.append(eachLine[start+1:end])
f_g.close()
return all_psw
#获取系统中所有用户的最后一次登陆时间
def getLastLogtime(user_name):
f_g = open(login_fpath, 'r')
user_logtime =
for eachLine in f_g:
if eachLine.startswith(user_name):
logtime = eachLine[eachLine.rfind('\\t')+1 : len(eachLine)-1]
user_logtime[user_name] = logtime
if user_logtime.has_key(user_name):
return user_logtime[user_name]
else:
return False
#给定两个时间(格式字符串:2016-12-09 09:23:08),如果间隔小于4h(14400s),返回True,否则返回False
def judge4H(time1, time2):
list1 = string.split(time1.replace('-','/').replace(' ','/').replace(':','/'),'/')
year1, month1, day1, hour1, minute1, second1 = int(list1[0]), int(list1[1]), int(list1[2]), int(list1[3]), int(list1[4]), int(list1[5])
time1 = datetime.datetime(year1, month1, day1, hour1, minute1, second1)
list2 = string.split(time2.replace('-','/').replace(' ','/').replace(':','/'),'/')
year2, month2, day2, hour2, minute2, second2 = int(list2[0]), int(list2[1]), int(list2[2]), int(list2[3]), int(list2[4]), int(list2[5])
time2 = datetime.datetime(year2, month2, day2, hour2, minute2, second2)
delta_days = abs(time1 - time2).days
if delta_days == 0:
if abs(time1 - time2).seconds <= 14400:
return True
else:
return False
else:
return False
#老用户登陆
def login():
log_re = True
print 'Please login!'
f_l = open(login_fpath, 'a+')
user_name = raw_input('Username: ').lower()
all_name = getAllName()
all_psw = getAllPsw()
if user_name in all_name:
temp0 = 0
for i in range(0, len(all_name)):
if all_name[i] == user_name:
temp0 = i
break
while True:
print 'Username: ',user_name
user_psw = encodePsw(raw_input('Password: '))
if user_psw == all_psw[temp0]:
this_logtime = str(datetime.datetime.now())[:19]
last_logtime = getLastLogtime(user_name)
if last_logtime != False:
if judge4H(this_logtime, last_logtime):
print 'In the past 4 hours, you\\'ve already login!'
print 'Your last logtime is: ',last_logtime
str0 = user_name + '\\t' + user_psw + '\\t' + this_logtime + '\\n'
f_l.write(str0)
f_l.close()
print 'Login finished!'
break
else:
print 'Password is incorrect! Please try again!'
else:
print 'Are you a new user? Please choose one from "y" and "n"!'
choice = ''
while True:
while True:
try:
choice = raw_input('Are you a new user? ').strip().lower()
except (IOError, EOFError, KeyboardInterrupt):
print 'Error choose! Please try again!'
if choice in 'yn':
break
if choice == 'y':
register()
log_re = False
break
if choice == 'n':
login()
break
return [log_re, user_name]
#老用户改密
def changePsw():
print 'Change password starting!'
list1 = login()
if list1[0] == True:
user_name = list1[1]
all_name = getAllName()
for i in range(0,len(all_name)):
if all_name[i] == user_name:
temp = i
all_psw = getAllPsw()
old_psw = all_psw[temp]
break
choice = ''
while True:
while True:
try:
choice = raw_input('Are you sure to change your password? Press "y" or "n"!').strip().lower()
except (IOError, KeyboardInterrupt, EOFError):
choice = 'n'
if choice not in 'yn':
continue
else:
break
if choice == 'n':
print 'Password isn\\'t changed!'
break
if choice == 'y':
new_psw1 = encodePsw(raw_input('Enter your new password: '))
new_psw2 = encodePsw(raw_input('Enter your new password again: '))
if new_psw1 == new_psw2 != old_psw:
f_c1 = open(regis_fpath, 'r')
regis_list = f_c1.readlines()
f_c1.close()
for i in range(0,len(regis_list)):
if regis_list[i].startswith(user_name):
del regis_list[i]
str0 = user_name + '\\t' + new_psw1 + '\\t' + str(datetime.datetime.now())[:19] + '\\t' + 'changePassword=True' + '\\n'
regis_list.append(str0)
break
f_c2 = open(regis_fpath, 'w')
for i in range(0,len(regis_list)):
f_c2.write(regis_list[i])
f_c2.close()
f_c3 = open(other_fpath, 'a+')
str1 = user_name + '\\t' + old_psw + '\\t' + new_psw1 + '\\t' + str(datetime.datetime.now())[:19] + '\\t' + 'class=changePassword' + '\\n'
f_c3.write(str1)
f_c3.close()
print 'Change password finished!'
break
else:
print 'Change password error! You have entered two different new passwords or new password is the same as the old one! '
else:
print 'Error! You may register a new acconut just before!'
#【仅限管理员】导出所有用户信息(用户名、(新)密码、上次登陆时间)
def viewUser():
print '[Notice!] This function only for administrator!'
name1 = raw_input('Administrator username: ')
psw1 = encodePsw(raw_input('Administrator password: '))
f_v1 = open('/Users/wangyu/Desktop/UMIS2.0/test_file/admin.txt', 'r')
for eachLine in f_v1:
temp0 = eachLine.find('\\t')
admin_name = eachLine[:temp0]
admin_psw = eachLine[temp0+1:]
f_v1.close()
if name1 == admin_name and psw1 == admin_psw:
all_name = getAllName()
all_psw = getAllPsw()
len0 = len(all_name)
f_v2 = open(admin_userInfo_fpath, 'a+')
for i in range(0,len0):
last_logtime = getLastLogtime(all_name[i])
if last_logtime == False:
f_v2.write(all_name[i] + '\\t' + all_psw[i] + '\\t' + 'null\\n')
else:
f_v2.write(all_name[i] + '\\t' + all_psw[i] + '\\t' + last_logtime + '\\n')
f_v2.close()
else:
print '[Warning] You have no permision!'
#删除用户(仅限管理员)
def delUser():
print '[Notice!] This function only for administrator!'
name1 = raw_input('Administrator username: ')
psw1 = encodePsw(raw_input('Administrator password: '))
f_v1 = open('/Users/wangyu/Desktop/UMIS2.0/test_file/admin.txt', 'r')
for eachLine in f_v1:
temp0 = eachLine.find('\\t')
admin_name = eachLine[:temp0]
admin_psw = eachLine[temp0 + 1:]
f_v1.close()
if name1 == admin_name and psw1 == admin_psw:
all_name = getAllName()
del_choice = ''
while True:
while True:
try:
del_choice = raw_input('Are you sure to delete a user? Press "y" or "n"!').strip().lower()
except (IOError, KeyboardInterrupt, EOFError):
del_choice = 'n'
if del_choice not in 'yn':
continue
else:
break
if del_choice == 'n':
print 'Don\\'t delete user!'
break
else:
del_name = raw_input('Enter the username which you want to delete: ')
if del_name in all_name:
#删除register_info.txt文件中该用户的记录(同一个用户名有且仅有一条记录)
f_vr1 = open(regis_fpath, 'r')
regis_list = f_vr1.readlines()
for i in range(0,len(regis_list)):
if regis_list[i].startswith(del_name):
del regis_list[i]
break
f_vr1.close()
f_vr2 = open(regis_fpath, 'w')
for i in range(0, len(regis_list)):
f_vr2.write(regis_list[i])
f_vr2.close()
#删除login_info.txt文件中该用户的记录(同一个用户名可能没有记录,可能有一条或多条记录)
f_vl1 = open(login_fpath, 'r')
login_list = f_vl1.readlines()
for i in range(0,len(login_list)):
if login_list[i].startswith(del_name):
login_list[i] = 'del'
f_vl1.close()
f_vl2 = open(login_fpath,'w')
for i in range(0,len(login_list)):
if login_list[i] <> 'del':
f_vl2.write(login_list[i])
f_vl2.close()
#删除other_info.txt文件中该用户的记录(同一个用户名可能没有记录,可能有一条或多条记录)
f_vo1 = open(other_fpath, 'r')
other_list = f_vo1.readlines()
for i in range(0,len(other_list)):
if other_list[i].startswith(del_name):
other_list[i] = 'del'
f_vo1.close()
f_vo2 = open(other_fpath, 'w')
for i in range(0,len(other_list)):
if other_list[i] <> 'del':
f_vo2.write(other_list[i])
f_vo2.close()
print 'Delete user finished!'
break
else:
print 'This user does not exists!'
break
else:
print '[Warning] You have no permision!'
#“主”函数
def showMenu():
print '*******************Micro User Information Management System V2.0*******************'
print ' [New User Register] Please press "r"!'
print ' [Old User Login] Please press "l"!'
print ' [Change Password] Pleas press "c"!'
print ' [View User] Only for administrator! Press "v"!'
print ' [Delete User] Only for administrator! Press "d"!'
print ' [Quit] Please press "q"!'
print '*****************************************************************************'
f1 = open(regis_fpath, 'a+')
f2 = open(login_fpath, 'a+')
f3 = open(other_fpath, 'a+')
f1.close()
f2.close()
f3.close()
choice = ''
choice_dict = 'r':register, 'l':login, 'c':changePsw, 'v':viewUser, 'd':delUser
while True:
while True:
try:
choice = raw_input('Please choose function: ').strip().lower()
except (EOFError, IOError, KeyboardInterrupt):
choice = 'q'
if choice not in 'rlcvdq':
print 'Error choose! Please try again!'
else:
break
if choice == 'q':
print 'We always waiting for you come back!'
break
choice_dict[choice]()
print '**********************************Next Choose**********************************'
if __name__ == '__main__':
showMenu()
=============================================================================================================================
五、新版本V2.1
1.磁盘文件存储形式优化
2.代码中改写磁盘文件那部分优化(一个f读,一个f写,看能否合二为一)
3.用户密码格式给出约束规范
……
以上是关于微型用户信息管理系统MUIMS(Micro User Information Management System)V2.0的主要内容,如果未能解决你的问题,请参考以下文章
微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0
微型用户信息管理系统MUIMS(Micro User Information Management System)V1.0