学习python——python实现简单的用户登录

  1 #!/usr/bin/env python
  2 import getpass
  3 import datetime
  4 global dic
  5 dic={"fentiao":"redhat","wang":"redhat"}
  6 time=[]
  7 global loginsig
  8 loginsig=0
  9 global loginuser
 10 def login():
 11         a=0
 12         b=0
 13         global loginsig
 14         global loginuser
 15         name = raw_input("Please input user:")
 16         lastlogintime=datetime.datetime(2017,3,30,21,02,4)
 17         password=""
 18         while (a<2):
 19                 if dic.has_key(name.lower()) == False:
 20                         name = raw_input("user error,input again:")
 21                         a=a+1
 22                 else:
 23                         break
 24         if a == 2 and dic.has_key(name.lower()) == False:
 25                 print "login filed!"
 26         else:
 27                 password = getpass.getpass("Please input password:")
 28                 while (b<2):
 29                         if password != dic.get(name.lower()):
 30                                 password = getpass.getpass("password error,    input again:")
 31                                 b=b+1
 32                         else:
 33                                 break
 34                 if b == 2 and password != dic.get(name.lower()):
 35                         print "login filed!"
 36                 else:
 37                         print "login successful"
 38                         loginsig=1
 39                         loginuser=name.lower()
 40                         t1=datetime.datetime.now()
 41                         time.append(t1)
 42                         print t1
 43                         if len(time) == 1:
 44                                 print "this is login first"
 45                         else:
 46                                 if (t1-time[len(time)-2]).seconds < 14400:
 47                                         print "you already logged at %s" %t    ime[len(time)-2]
 48                         print loginsig
 49                         showmenu()
 50
 51 def showuser():
 52         if loginsig == 0:
 53                 print "please login first!"
 54         if loginsig == 1:
 55                 for i in range(0,len(dic.keys())):
 56                         print dic.keys()[i],"\n"
 57         showmenu()
 58 def remove():
 59         global dic
 60         dic={"fentiao":"redhat","wang":"redhat"}
 61         if loginsig == 0:
 62                 print "please login first!"
 63         if loginsig == 1:
 64                 if loginuser == "fentiao":
 65                         for i in range(0,len(dic.keys())):
 66                                 print dic.keys()[i],"\n"
 67                         delname = raw_input("you want to delete:")
 68                         dic.pop(delname)
 69                         print delname,"deleted"
 70                 else:
 71                         print "you have no permission to rmove user!"
 72         showmenu()
 73
 74
 75
 76 def showmenu():
 77         print """
 78                 1.login
 79                 2.remove
 80                 3.showuser
 81                 4.quit"""
 82         num = input("your choice:")
 83         while num not in [1,2,3,4]:
 84                 num = raw_input("please choose in 1, 2 ,3 or 4:")
 85                 if num in [1,2,34]:
 86                         break
 87         if num == 1:
 88                 login()
 89         elif num == 2:
 90                 remove()
 91         elif num == 3:
 92                 showuser()
 93         else:
 94                 return
 95
 96 showmenu()

运行:

1.测试登录:

不区分用户名的大小写,

若上次登录与本次登录不超过4小时则,输出“you already logged at :上次的时间”

学习python——python实现简单的用户登录_第1张图片

2.显示用户信息

学习python——python实现简单的用户登录_第2张图片

没有登录时提醒用户登录后再查询

学习python——python实现简单的用户登录_第3张图片

3.删除某个用户,只有当用户为fentiao时,才可删除,其他不可

学习python——python实现简单的用户登录_第4张图片

若为其他用户要删除,则提醒,不允许删除

学习python——python实现简单的用户登录_第5张图片

你可能感兴趣的:(python)