Python login interface writing

need:

  1. Enter username and password
  2. After the authentication is successful, a welcome message is displayed.
  3. Lock user after typing wrong three times

Readme:

  1.ID.txt is a file that stores usernames and passwords. Like linux, it is separated by colons. Compared with spaces and tabs, colons are better handled.

    sam:123

    jack:456

    tom:789

  2. LockID.txt is the document that stores the locked user name, and it is empty by default.

  3. When the user logs in, it will first determine whether it is in the lock file, if so, it will prompt that it is locked and exit the program

  4. Next, judge whether the user ID is legal or not. The legal user will be locked if the password is wrong 3 times;

flow chart:

    

 

Code:

  

#-*- coding:utf-8 -*-
#Version:python3.5
#Programme Summary: If the password is wrong 3 times, the corresponding user name will be locked; if the user name is wrong 3 times, the program will be logged out.

import sys,getpass
#File path using variables, easy to change
LockID = 'I:\python_test\LockID.txt'
ID = 'I:\python_test\ID.txt'
#Define the function to lock the user
def Deny_ID(username):
    with open(LockID,'a') as Deny_f:
        Deny_f.write('\n' + username)
        sys.exit("Wrong password 3 times, account is locked!")

ID_list=[]
count = 0
#Use the list to get the user ID, which is convenient to judge whether the user is legal or not.
with open(ID, 'r') as ID_file:
    for lines in ID_file:
        (usr, pawd) = lines.strip().split(':')
        ID_list.append(usr)
#Main program 3 loops
while count < 3:
    count += 1
    username = input('username:')
    #Use the with method to read the document to prevent forgetting to close the file later
    with open(LockID,'r') as Lock_f:
       #for loop judges whether the user is in the lock table, if so, the prompt is locked and the program exits
        for line in Lock_f:
            if username == line.strip('\n'):
                sys.exit("This user has been locked, please find the administrator.")
       #Check if the username is valid
        if username in ID_list:
            p = 0
            #If the username is valid, then determine whether the password is correct
            while p < 3:
                p +=1
                password = getpass.getpass("password:")
                with open(ID,'r') as ID_f:
                    for lines in ID_f:
                        #Separate with colon and get user id and password
                        (user,passwd) = lines.strip().split(':')
                        # Determine whether the user ID and password are established at the same time
                        if user == username and passwd == password:
                            sys.exit("Welcome user %s to login!"%username)
                    else:
                        if p != 3:
                            print("The password is wrong for the %dth time, the 3rd time will be locked" %p)
                        else:
                            #call lock user function
                            Deny_ID(username)
     #Illegal user ID
        else:
            if count != 3:
                print("Username error %d time."%count)
            else:
                #Error user id will not be written to lock table
                sys.exit("The user name is wrong 3 times, the program is logged out!")

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324763665&siteId=291194637