newlogin.py
1 #!/bin/bash/python 2 #coding=utf-8 3 import getpass 4 import time 5 import re 6 import os 7 #Logintime=time.asctime(time.localtime(time.time())) 8 dic={"fentiao":"westos","wang":"wang","li":"li"} 9 logintime={} 10 def deluserFunc():#删除用户 11 while 1: 12 for k in dic.keys(): 13 print "username:%s" %k 14 username=raw_input("input username:") 15 if dic.has_key(username) == True and username != getusername2: 16 del dic[username] 17 print "DONE" 18 while 1: 19 ex=raw_input("exit?(y/n)") 20 if ex == "y": 21 print "bye" 22 exit(0) 23 elif ex == "n": 24 menuFunc() 25 else: 26 print "input error" 27 else: 28 print "[username error!]" 29 30 31 def showuserinfoFunc():#显示用户信息 32 for (k,v) in dic.items(): 33 print "username:%s/ password:%s" %(k,v) 34 menuFunc() 35 def menuFunc():#显示菜单 36 print ''' 37 (D)elete user 38 (S)how userinfo 39 (E)xit 40 ''' 41 choice=raw_input("input you choice:").upper() 42 while 1: 43 if choice == "D": 44 deluserFunc() 45 elif choice == "S": 46 showuserinfoFunc() 47 elif choice == "E": 48 print "bye" 49 exit(0) 50 else: 51 print "input error!" 52 53 54 def loginFunc():#用户登陆 55 j=0 56 i=3 57 while 1: 58 getusername=raw_input("input your username:") 59 m=re.search("\W+",getusername ) 60 if m is None : 61 global getusername2 62 getusername2=getusername.lower() 63 if dic.has_key(getusername2) == True: 64 while j < 3: 65 getpasswd=getpass.getpass("input your pass:") 66 if getpasswd == dic.get(getusername2) : 67 loginTime=time.time()#本次登陆时间 68 if os.path.exists(getusername2) == False : 69 vimtime=open(getusername2,'w+') 70 vimtime.write('0') 71 vimtime.close() 72 fotime=open(getusername2,'r') 73 lastLogintime1=fotime.read()#读取上次登陆时间 74 fotime.close() 75 lastLogintime2=time.ctime(float(lastLogintime1))#转换上次登陆时间格式 76 if float(loginTime)-float(lastLogintime1) < 14400:#若登陆时间相差不超过4小时 77 print "welcome,you are already logged in %s" %lastLogintime2 78 else: 79 print "welcome" 80 fo=open(getusername2,'w+') 81 fo.write('%s\n' %(loginTime)) 82 fo.close() 83 menuFunc() 84 else: 85 i-=1 86 print "password error,you have %d chance" %i 87 j+=1 88 if j==3: 89 exit(0) 90 elif len(getusername2) >1 : 91 adduser=raw_input("username unexist,do you want to add?(y/n)")#添加新用户 92 while 1: 93 if adduser == "y": 94 while 1: 95 newpasswd=getpass.getpass("input newuser %s 's password:" %getusername)#输入密码 96 newpasswdagain=getpass.getpass("input password again:") 97 if newpasswd == newpasswdagain: 98 dic[getusername]=newpasswd 99 print "useradd success" 100 loginFunc() 101 else: 102 print "error,password different!" 103 elif adduser == "n": 104 loginFunc() 105 else: 106 print "[username lenth must > 1!]" 107 else: 108 print "[input error,username must make up of letters or numbers!]" 109 110 if __name__ == '__main__': 111 loginFunc()