首页 > 代码库 > python 简单的用户登陆
python 简单的用户登陆
#!/usr/bin/env python #coding:utf-8 import hashlib from datetime import datetime ul = {} def newuser(): a = [] prompt = "Please enter username again:" while True: name = raw_input(prompt).lower() if not name.isalnum() and ‘‘ in name : print(‘name format error‘) continue else : if ul.has_key(name): prompt = "username already exists" continue else: break passwd = raw_input(‘enter password:‘) m=hashlib.md5() m.update(passwd) a.append(m.hexdigest()) a.append(datetime.now()) ul[name]=a print ‘new user is %s ‘ ‘regiter time is %s‘ %(name,ul[name][1]) def olduser(): name= raw_input(‘Please enter username again:‘).lower() passwd = raw_input(‘Please enter your password:‘) m=hashlib.md5() m.update(passwd) pwd = ul.get(name) if pwd[0]==m.hexdigest(): newtime = datetime.now() if (newtime-ul[name][1]).days == 0 and (newtime-ul[name][1]).seconds < 14400: print ‘you already logged in at %s: ‘ %(ul[name][1]) else: pwd[1]=newtime print ‘welcome back %s, login time is %s‘ %(name,passwd[1]) else: print ‘login incorrect‘ def removeuser(): print ul name=raw_input(‘input a user name to remove: ‘).lower() if name in ul: ul.pop(name) print ‘remove successful‘ else: print ‘this uesr not exist‘ def login(): while True: name=raw_input(‘Please enter username:‘).lower() if not name.isalnum() and ‘‘ in name: print ‘name format error‘ continue else: if not ul.has_key(name): print ‘user name is not in userlist‘ answer=raw_input(‘register a new user? (y/n):‘).lower() if ‘y‘==answer: newuser() break elif ‘n‘==answer: break else: print ‘user name is already in db‘ olduser() break def showmenu(): prompt = """ (U)ser login (R)emove 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 ‘urq‘: print("invalid option,try again") else : chosen = True if choice == ‘q‘:done = True if choice == ‘u‘:login() if choice == ‘r‘:removeuser() if __name__ == ‘__main__‘: showmenu()
python 简单的用户登陆
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。