您的位置:首页 > 编程语言 > Python开发

Python核心编程 第七章 练习7–5

2015-04-04 18:42 369 查看
7–5. userpw2.py. 下面的问题和例题7.1 中管理名字-密码的键值对数据的程序有关。

(a)修改那个脚本,使它能记录用户上次的登录日期和时间(用time 模块),并与用户密码一起 保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示, 在户名成功登录后,应更新相应用户的上次登录时间戳。如果本次登录与上次登录在时间上相差不 超过4 个小时,则通知该用户: “You already logged in at: .”

(b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名 字和他们的密码的清单。

(c) 口令目前没有加密。请添加一段对口令加密的代码(请参考crypt, rotor, 或其它加密模块)

(d) 为程序添加图形界面,例如,用Tkinter 写。

(e) 要求用户名不区分大小写。

(f) 加强对用户名的限制,不允许符号和空白符。

(g)合并“新用户”和“老用户”两个选项。如果一个新用户试图用一个不存在的用户名登录,

询问该用户是否是新用户,如果回答是肯定的,就创建该帐户。否则,按照老用户的方式登录。

原原本本的代码:

#!/usr/bin/env python

db = {}

def newuser():
prompt = 'login desired: '
while True:
name = raw_input(prompt)
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
pwd = raw_input('passwd: ')
db[name] = pwd

def olduser():
name = raw_input('login: ')
pwd = raw_input('passwd: ')
passwd = db.get(name)
if passwd == pwd:
print 'welcome back', name
else:
print 'login incorrect'

def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(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 'neq':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'n': newuser()
if choice == 'e': olduser()

if __name__ == '__main__':
showmenu()


(a)修改那个脚本,使它能记录用户上次的登录日期和时间(用time 模块),并与用户密码一起 保存起来。程序的界面有要求用户输入用户名和密码的提示。无论户名是否成功登录,都应有提示, 在户名成功登录后,应更新相应用户的上次登录时间戳。如果本次登录与上次登录在时间上相差不 超过4 个小时,则通知该用户: “You already logged in at: .”

#!/usr/bin/env python

from time import time,ctime #导入模块
db = {}

def newuser():
prompt = 'login desired: '
while True:
name = raw_input(prompt)
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
pwd = raw_input('passwd: ')#用户名合格后,要求输入密码
logt=time()#获取时间戳
db[name]=[pwd,logt]#把密码和时间组成列表存到字典,共享一个键

def olduser():
name=raw_input('login: ')
pwd=raw_input('passwd: ')
if name in db:#检测是否是老用户
if pwd == db.get(name)[0]: #获取老用户密码与输入密码比较
print 'welcome back',name #显示欢迎信息
print 'You lasttime logged in at:',ctime(db[name][1])
#显示上次登录的时间戳
current=time()#获取当前时间
delta=current-db[name][1]#求上次登录与现在时间的时间差
if delta<=14400: #判断是否在4小时内
print 'You already logged in 4 hours period!'

else:#若时间差不在四小时之内
logt=time()#获取当前时间
db[name][1]=logt #把密码和时间组成列表存到字典,共享一个键,更新时间戳
else: #密码错误
print 'login incorrect'

else: #不是老用户
print 'login incorrect'

def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(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 'neq':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'n': newuser()
if choice == 'e': olduser()

if __name__ == '__main__':
showmenu()


(b) 添加一个“管理”菜单,其中有以下两项:(1)删除一个用户 (2)显示系统中所有用户的名字和他们的密码的清单。

#!/usr/bin/env python

from time import time,ctime #导入模块
db = {}

def newuser():
prompt = 'login desired: '
while True:
name = raw_input(prompt)
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
pwd = raw_input('passwd: ')
logt=time()
db[name]=[pwd,logt]

def olduser():
name=raw_input('login: ')
pwd=raw_input('passwd: ')
if name in db:
if pwd == db.get(name)[0]:
print 'welcome back',name
print 'You lasttime logged in at:',ctime(db[name][1])
current=time()
delta=current-db[name][1]
if delta<=14400: #判断是否在4小时内
print 'You already logged in 4 hours period!'

else:
logt=time()#获取当前时间
db[name][1]=logt #把密码和时间组成列表存到字典,共享一个键
else:
print 'login incorrect'

else:
print 'login incorrect'

def deluser():
Getuserpwd()
c=raw_input('del a user,its name:')
if c in db.keys():
del db[c]
print 'After del:'
Getuserpwd()
else:
print 'Wrong choose!'

def Getuserpwd():
if db:
for name in db:
print 'name:',name,'pwd:',db[name][0]
else:
print 'No usr!'

def management():
prompt = """
(D)el a user
(G)et all user and pwd
(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 'gdq':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'd': deluser()
if choice == 'g': Getuserpwd()

def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(M)anagement
(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 'neqm':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'n': newuser()
if choice == 'e': olduser()
if choice == 'm': management()

if __name__ == '__main__':
showmenu()


c)口令目前没有加密. 请添加一段对口令加密的代码(请参考crypt,rotor,或其它加密模块)

#!/usr/bin/env python

from time import time,ctime #导入模块
import base64#导入加密模块
db = {}

def newuser():
prompt = 'login desired: '
while True:
name = raw_input(prompt)
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
p1 = raw_input('passwd: ')#明文密码
logt=time()
p2 = base64.encodestring(p1)#密文密码
db[name]=[p2,logt]#存储密文密码

def olduser():
name=raw_input('login: ')
p1=raw_input('passwd: ')
if name in db:
p2=base64.decodestring(db[name][0])#解密所存密文
if p1 == p2 : #与用户输入密码比对
print 'welcome back',name
print 'You lasttime logged in at:',ctime(db[name][1])
current=time()
delta=current-db[name][1]
if delta<=14400: #判断是否在4小时内
print 'You already logged in 4 hours period!'

else:
logt=time()#获取当前时间
db[name][1]=logt #把密码和时间组成列表存到字典,共享一个键
else:
print 'login incorrect'

else:
print 'login incorrect'

def deluser():
Getuserpwd()
c=raw_input('del a user,its name:')
if c in db.keys():
del db[c]
print 'After del:'
Getuserpwd()
else:
print 'Wrong choose!'

def Getuserpwd():
if db:
for name in db:
print 'name:',name,'pwd:',db[name][0]
else:
print 'No user!'

def management():
prompt = """
(D)el a user
(G)et all user and pwd
(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 'gdq':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'd': deluser()
if choice == 'g': Getuserpwd()

def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(M)anagement
(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 'neqm':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'n': newuser()
if choice == 'e': olduser()
if choice == 'm': management()

if __name__ == '__main__':
showmenu()


先贴一下前几问的输入输出:

>>>
#主菜单
(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit
#创建三个用户
Enter choice: n

You picked:

login desired: 1
passwd: 123

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit

Enter choice: n

You picked:

login desired: a
passwd: xyz

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit

Enter choice: n

You picked:

login desired: name999
passwd: 999name

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit
#老用户输出错误密码的情况
Enter choice: e

You picked: [e]
login: 1
passwd: abc
login incorrect

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit
#错误菜单选项的情况
Enter choice: 1

You picked: [1]
invalid menu option, try again

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit
#老用户登录成功,显示上次登录时间,若在四小时内,打印提示信息
Enter choice: e

You picked: [e]
login: 1
passwd: 123
welcome back 1
You lasttime logged in at: Sat Apr 04 17:14:15 2015
You already logged in 4 hours period!

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit
#管理菜单中的两个子菜单
Enter choice: m

You picked: [m]

(D)el a user
(G)et all user and pwd
(Q)uit

Enter choice: g
#获得所有用户和对应密码,密码是密文显示
You picked: [g]
name: 1 pwd: MTIz

name: a pwd: eHl6

name: name999 pwd: OTk5bmFtZQ==

(D)el a user
(G)et all user and pwd
(Q)uit

Enter choice: d
#删除一个用户
You picked: [d]
name: 1 pwd: MTIz

name: a pwd: eHl6

name: name999 pwd: OTk5bmFtZQ==

del a user,its name:a
After del:
name: 1 pwd: MTIz

name: name999 pwd: OTk5bmFtZQ==

(D)el a user
(G)et all user and pwd
(Q)uit
#退出管理菜单
Enter choice: q

You picked: [q]

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit
#退出总菜单
Enter choice: q

You picked: [q]
>>>


(d) 为程序添加图形界面,例如,用Tkinter 写。

正在收集资料,学习tkinter,以完成作业

(e) 要求用户名不区分大小写。

思路:菜单选项的写法给我启发,设定将用户名输入转换为小写就好。

#!/usr/bin/env python

from time import time,ctime #导入模块
import base64
db = {}

def newuser():
prompt = 'login desired: '
while True:
name = raw_input(prompt).strip()[0].lower()#设定小写
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
p1 = raw_input('passwd: ')
logt=time()
p2 = base64.encodestring(p1)
db[name]=[p2,logt]

def olduser():
name=raw_input('login: ').strip()[0].lower()#设定小写
p1=raw_input('passwd: ')
if name in db:
p2=base64.decodestring(db[name][0])
if p1 == p2 :
print 'welcome back',name
print 'You lasttime logged in at:',ctime(db[name][1])
current=time()
delta=current-db[name][1]
if delta<=14400: #判断是否在4小时内
print 'You already logged in 4 hours period!'

else:
logt=time()#获取当前时间
db[name][1]=logt #把密码和时间组成列表存到字典,共享一个键
else:
print 'login incorrect'

else:
print 'login incorrect'

def deluser():
Getuserpwd()
c=raw_input('del a user,its name:').strip()[0].lower()#设定小写
if c in db.keys():
del db[c]
print 'After del:'
Getuserpwd()
else:
print 'Wrong choose!'

def Getuserpwd():
if db:
for name in db:
print 'name:',name,'pwd:',db[name][0]
else:
print 'No user!'

def management():
prompt = """
(D)el a user
(G)et all user and pwd
(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 'gdq':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'd': deluser()
if choice == 'g': Getuserpwd()

def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(M)anagement
(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 'neqm':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'n': newuser()
if choice == 'e': olduser()
if choice == 'm': management()

if __name__ == '__main__':
showmenu()


(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit

Enter choice: n

You picked:

login desired: a
passwd: 1

(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit

Enter choice: e

You picked: [e]
login: A
passwd: 1
welcome back a
You lasttime logged in at: Sat Apr 04 19:12:15 2015
You already logged in 4 hours period!


(f) 加强对用户名的限制,不允许符号和空白符。

思路:除了符号和空白符。那就理解为用户名应该由 数字和大小写字母组成。

#!/usr/bin/env python

from time import time,ctime #导入模块
import base64
import string #导入string模块
db = {}
y=string.letters+string.digits#限定只能是数字和大小写字母

def newuser():
prompt = 'login desired,only accept digits and letters: '
while True:
name = raw_input(prompt).strip()[0].lower()
if name in y:#验证
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
else:
print 'Invalid name!'
p1 = raw_input('passwd: ')
logt=time()
p2 = base64.encodestring(p1)
db[name]=[p2,logt]

def olduser():
name=raw_input('login: ').strip()[0].lower()
p1=raw_input('passwd: ')
if name in db:
p2=base64.decodestring(db[name][0])
if p1 == p2 :
print 'welcome back',name
print 'You lasttime logged in at:',ctime(db[name][1])
current=time()
delta=current-db[name][1]
if delta<=14400: #判断是否在4小时内
print 'You already logged in 4 hours period!'

else:
logt=time()#获取当前时间
db[name][1]=logt #把密码和时间组成列表存到字典,共享一个键
else:
print 'login incorrect'

else:
print 'login incorrect'

def deluser():
Getuserpwd()
c=raw_input('del a user,its name:').strip()[0].lower()
if c in db.keys():
del db[c]
print 'After del:'
Getuserpwd()
else:
print 'Wrong choose!'

def Getuserpwd():
if db:
for name in db:
print 'name:',name,',pwd:',db[name][0]
else:
print 'No user!'

def management():
prompt = """
(D)el a user
(G)et all user and pwd
(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 'gdq':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'd': deluser()
if choice == 'g': Getuserpwd()

def showmenu():
prompt = """
(N)ew User Login
(E)xisting User Login
(M)anagement
(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 'neqm':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'n': newuser()
if choice == 'e': olduser()
if choice == 'm': management()

if __name__ == '__main__':
showmenu()


(N)ew User Login
(E)xisting User Login
(M)anagement
(Q)uit

Enter choice: n

You picked:

login desired,only accept digits and letters: @1
Invalid name!
login desired,only accept digits and letters: a
passwd: 1


(g)合并“新用户”和“老用户”两个选项。如果一个新用户试图用一个不存在的用户名登录,询问该用户是否是新用户,如果回答是肯定的,就创建该帐户。否则,按照老用户的方式登录。

思路:释放了原来的两个函数的直接调用

创建一个Userlog函数来一次调用就行了。添加一些判断。

#!/usr/bin/env python

from time import time,ctime #导入模块
import base64
import string
db = {}
y=string.letters+string.digits

def newuser():
prompt = 'login desired,name only accept digits and letters: '
while True:
name = raw_input(prompt).strip()[0].lower()
if name in y:
if db.has_key(name):
prompt = 'name taken, try another: '
continue
else:
break
else:
print 'Invalid name!'
p1 = raw_input('passwd: ')
logt=time()
p2 = base64.encodestring(p1)
db[name]=[p2,logt]

def olduser():
name=raw_input('login: ').strip()[0].lower()
p1=raw_input('passwd: ')
if name in db:
p2=base64.decodestring(db[name][0])
if p1 == p2 :
print 'welcome back',name
print 'You lasttime logged in at:',ctime(db[name][1])
current=time()
delta=current-db[name][1]
if delta<=14400: #判断是否在4小时内
print 'You already logged in 4 hours period!'
else:
logt=time()#获取当前时间
db[name][1]=logt #把密码和时间组成列表存到字典,共享一个键
else:
print 'login incorrect'

else:#当输入的用户名和密码不是注册过的,打印提升信息:是否注册?
w=raw_input('register it(Y/N)?').strip()[0].lower()
if 'y' == w:
newuser()#若选择注册,调用新用户注册函数
else:
print 'login incorrect'

def userlog():#userlog函数首先默认调用老用户登录函数
olduser()

def deluser():
Getuserpwd()
c=raw_input('del a user,its name:').strip()[0].lower()
if c in db.keys():
del db[c]
print 'After del:'
Getuserpwd()
else:
print 'Wrong choose!'

def Getuserpwd():
if db:
for name in db:
print 'name:',name,',pwd:',db[name][0]
else:
print 'No user!'

def management():
prompt = """
(D)el a user
(G)et all user and pwd
(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 'gdq':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'd': deluser()
if choice == 'g': Getuserpwd()

def showmenu():
prompt = """
(U)ser Login
(M)anagement
(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 'uqm':
print 'invalid menu option, try again'
else:
chosen = True

if choice == 'q': done = 1
if choice == 'u': userlog()
if choice == 'm': management()

if __name__ == '__main__':
showmenu()


(U)ser Login
(M)anagement
(Q)uit

Enter choice: u

You picked: [u]
login: 1
passwd: 1
register it(Y/N)?y
login desired,name only accept digits and letters: pythON
passwd: c++

(U)ser Login
(M)anagement
(Q)uit

Enter choice: u

You picked: [u]
login: PYthon
passwd: c++
welcome back p
You lasttime logged in at: Sat Apr 04 19:55:51 2015
You already logged in 4 hours period!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐