简单的登录系统—见《python核心编程》ch7
这个程序管理用于登录系统的用户信息:登录名字和密码。登录用户账号建立后,已存在用户可以用登录名字和密码重新返回系统,新用户必须登记注册。
《python核心编程》里给出的示例代码如下:
1 #!/usr/bin/env python 2 3 db = {} 4 5 def newuser(): 6 prompt = 'login desired: ' 7 while 1: 8 name = raw_input(prompt) 9 if db.has_key(name): 10 prompt = 'name taken, try another: ' 11 continue 12 else: 13 break 14 pwd = raw_input('passwd: ') 15 db[name] = pwd 16 17 def olduser(): 18 name = raw_input('login: ') 19 pwd = raw_input('passwd: ') 20 passwd = db.get(name) 21 if passwd == pwd: 22 pass 23 else: 24 print 'login incorrect' 25 return 26 27 print 'welcome back', name 28 29 def showmenu(): 30 prompt = """ 31 (N)ew User Login 32 (E)xisting User Login 33 (Q)uit 34 35 Enter choice: """ 36 37 done = 0 38 while not done: 39 chosen = 0 40 while not chosen: 41 try: 42 choice = raw_input(prompt)[0] 43 except (EOFError, KeyboardInterrupt): 44 choice = 'q' 45 print '\nYou picked: [%s]' % choice 46 47 if choice not in 'neq': 48 print 'invalid menu option, try again' 49 else: 50 chosen = 1 51 52 if choice == 'q': done = 1 53 if choice == 'n': newuser() 54 if choice == 'e': olduser() 55 56 if __name__ == '__main__': 57 showmenu()
而看到第7章习题,对应这段代码作出修改,使其更加健壮,我的水平有限,只做了几处的修改:
1. 在存储的字典db里加入了时间记录,也就是可以记录登录时间。
2.使用了shevle模块对数据储存在磁盘,这一点在大数据里比较重要,总不能把所有数据都存贮在内存中。
3.把新旧用户函数合并起来形成了一个User函数。
4.增加了一个菜单管理函数,其功能可以删除用户,显示用户。
修改后的代码如下:
1 #userpw.py 2 3 import time 4 import shelve 5 6 db = shelve.open('database.dat') 7 db.close() 8 9 def user(): 10 choice1=raw_input('Are you a new user? Please enter choose y(for yes) or n(for no):' ) 11 if choice1=='y': 12 choice2=raw_input('register your name? y or n?:' ) 13 if choice2=='y': 14 name=raw_input('login desized:' ) 15 pwd=input('password:' ) 16 db = shelve.open('database.dat') 17 db[name]=[pwd, time.time()] 18 db.close() 19 return 20 elif choice2=='n': 21 return 22 else: 23 print "invalid option, try again" 24 return 25 26 else: 27 name=raw_input('login:' ) 28 pwd=input('password:') 29 db = shelve.open('database.dat') 30 passwd=db.get(name)[0] 31 print passwd 32 if passwd==pwd: 33 db[name]=[pwd, time.time()] 34 pass 35 else: 36 print 'login incorrect.' 37 return 38 print 'Welcome back', name 39 db.close() 40 41 def managemenu(): 42 db = shelve.open('database.dat') 43 choice = raw_input('Please enter your choice(d or s): ') 44 if choice=='d': 45 name=raw_input('Enter the name you want to delete: ') 46 popname=db.pop(str(name)) 47 elif choice=='s': 48 if db=={}: 49 print "There is no data." 50 else: 51 for key in db: 52 print '%s:%s'%(key,db[key]) 53 54 else: 55 print 'invalid menu option, try again' 56 managemenu() 57 db.close() 58 59 def showmenu(): #显示 60 prompt = """ 61 62 (U)ser 63 (M)anagemenu 64 (Q)uit 65 66 Enter choice: """ 67 68 done = 0 69 while not done: 70 chosen = 0 71 while not chosen: 72 try: 73 choice = raw_input(prompt)[0] 74 except (EOFError, KeyboardInterrupt): 75 choice = 'q' 76 print '\nYou picked: [%s]' % choice 77 78 if choice not in 'uqm': 79 print 'invalid menu option, try again' 80 else: 81 chosen = 1 82 83 if choice == 'q': done = 1 84 if choice == 'u': user() 85 if choice == 'm': managemenu() 86 87 88 89 if __name__ == '__main__': 90 showmenu()
除此之外,还有很多地方需要改进的。稍后再做改进。
1.密码没有进行加密。
2.使用shelve模块可以很方便地访问和调用,但是对于大数据是不是应该使用数据库呢?
3.面向对象。
4.GUI
等等可以改进~!!!
浙公网安备 33010602011771号