首页 > 代码库 > 用户登陆管理

用户登陆管理







#!/usr/bin/env python
#coding:utf-8


‘‘‘
Program:
    User login, three chances after the wrong
    There have some manages for user logon
History:
2017.03.29      liuting      First release
‘‘‘

import getpass
import os
from datetime import datetime

f=open("login_time.txt","r")
login_time = f.read()

dic = {"student":"student","root":"westos","lt":"zq"}
dic_time = {"username":"login_time"}
username = raw_input("Please input the username:")

login_time_now = datetime.now()
a = datetime.strptime(str(login_time_now),"%Y-%m-%d %H:%M:%S.%f")
b = datetime.strptime(str(login_time),"%Y-%m-%d %H:%M:%S.%f")
x = (a-b).seconds

if dic.has_key(username) == False:
    print "The user not exist!"
elif x <= 14400:
    print "You already logged in at:%s\n" %(login_time)
else:
    for i in range(0,3):
        password = getpass.getpass("Please input the password:")
        if  password == dic.get(username):
            print "Login success!"
            print "Last login time: %s" %(str(login_time))
            f=open("login_time.txt","w")
            f.write(datetime.now())
            f.close()
            break
        elif i == 2:
            print "Sorry,Please login again."
        else:
            print "Password error,try again."
            print "%d time(s) for input" %(2-i)
while username == "root":
    operation = raw_input(" D(delete user)\n S(show usermessage)\n A(add user)\n L(list existing user)\n q(quit)\n What operation do you want:\n")
    if operation == "D":
        name = raw_input("Please input the username youwant to delete:")
        if dic.has_key(name) == True:
            dic.pop(name)
            print "Delete success!"
        else:
            print "The user not exist!"
    elif operation == "S":
        name = raw_input("Please input the username you want to see:")
        print "password:%s\n login_time:%s\n **************************" %(dic.get(name,"The user not exist"),dic_time.get(name))
    elif operation == "A":
        name = raw_input("Please input the username you want to add:")
        for i in range(0,3):
            passwd_first = getpass.getpass("Please input a password:")
            passwd_second = getpass.getpass("Please input the password again:")
            if passwd_first == passwd_second:
                dic[name] = passwd_first
                print "Useradd success!\n **************************"
                break
            elif i == 2:
                print "Please useradd again.\n \n"
            else:
                print "Password input different,try again!\n %d times for you!\n \n" %(2 - i)
    elif operation == "L":
        print "Existing user:\n %s\n **************************" %(dic.keys())
    elif operation == "q":
        break
    else:
        print "Unknown operation!\n \n"


本文出自 “12449513” 博客,请务必保留此出处http://12459513.blog.51cto.com/12449513/1912415

用户登陆管理