赞
踩
db={}
def manage_list():
prompt="""
(D)elete user
(P)rint all user
(Q)uit
Enter choice:"""
done=False
while not done:
chosen=False
while not chosen:
try:
choice=raw_input(prompt).strip()[0].lower()
except (EOFError,KeyboardInterrupt):
choice='q'
print'\nYou picked:[%s]'%choice
if choice not in 'dpq':
print'invalid option,try again'
else:
chosen=True
if choice=='q':done=True
if choice=='d':delete_user()
if choice=='p':print_user()
def delete_user():
i=0
user_list=[]
for each_user in db.keys():
user_list.append(each_user)
print '['+str(i)+']'+str(each_user)
i+=1
user_number=input('please choice an user number to delete an user:')
if 0<=user_number
print'your choice is:'+user_list[user_number]
confirm=raw_input('do you want to del this user:[Y/N]')
if confirm == 'Y':
del db[user_list[user_number]]
else:
manage_list()
else:
print 'please input the right number!'
delete_user()
def print_user():
i=0
for each_user in db.items():
print '['+str(i)+']'+'username:'+each_user[0]+' userpwd:'+each_user[1][0]
i+=1
def last_login_timestamp(name):
import time
last_time=db.get(name)[1]
if not last_time:
db[name][1]=time.time()
print'welocme back',name
#print'time is: '+time.strftime('%y-%m-%d %H:%M:%S',time.localtime(db.get(name)[1]))
else:
if 0
print'you already logged in at:'+time.strftime('%y-%m-%d %H:%M:%S',time.localtime(db.get(name)[1]))
def md5(str):
import hashlib
m = hashlib.md5()
m.update(str)
return m.hexdigest()
def login():
name=raw_input('plsase input your username:').lower()
if db.has_key(name):
olduser(name)
else:
confirm=raw_input('your username is not created,do you want to creat this user?[Y/N]')
if confirm=='Y':
newuser()
else:
olduser()
def newuser():
import re
prompt='login desired:'
while True:
name=raw_input(prompt).lower()
if not re.match('^[0-9a-z]+$',name):
print'your name is wrong! please try again!'
newuser()
if db.has_key(name):
prompt='name taken, try another:'
continue
else:
break
L_pwd=raw_input('passwd:')
pwd=md5(L_pwd)
last=0
db[name]=[pwd,last]
def olduser(name):
user_name=name
L_pwd=raw_input('passwd:')
pwd=md5(L_pwd)
passwd= db.get(user_name)[0]
if passwd==pwd:
last_login_timestamp(user_name)
else:
print 'login incorrect'
def showmenu():
prompt="""
(L)login
(Q)uit
(M)anage
Enter choice:"""
done=False
while not done:
chosen=False
while not chosen:
try:
choice=raw_input(prompt).strip()[0].lower()
except (EOFError
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。