Three login freeze operations

Requirement:

1. Store the password of the user in the form of ciphertext

2. After the user has entered the wrong account three times, enter the account again to display 'the account has been frozen, unable to log in'

Train of thought:

1. First, create two files under the path of running script: one file with user information and one file storing frozen users

2. User registration first: need to determine whether the user name exists

3. User login:

- first judge whether the user input is in the frozen file

- if not, continue to match user name and password

- write the user name to the frozen file when the user enters the wrong account or password three times

 

The code is as follows:

import hashlib

def get_md5(user,pwd):
    md5 = hashlib.md5(user.encode('utf-8'))  # md5 algorithm encryption
    md5.update(pwd.encode('utf-8'))
    return md5.hexdigest()

def register():
    '''
    //User registration
    :return:
    '''
    while True:
        print('*****User registration*****')
        user = input('Please enter account(input n Return to upper level):')
        if user.upper() == 'N': return
        pwd = input('Please input a password:')
        flag = False
        with open('userinfo',mode='r',encoding='utf-8') as f:  #  Open user file,Determine whether the user name exists
            for line in f:
                if user in line.strip().split('|'):
                    flag = True
        if flag:
            print('User name already exists,Please re-enter')
        else:
            with open('userinfo',mode='a',encoding='utf-8') as f:  # Write user registration information to user file
                f.write('%s|%s\n' %(user,get_md5(user,pwd)))  # Store the user's password in the form of ciphertext
                print('\033[1;31m login was successful\033[0m')

def login():
    '''
    //User Login
    :return:
    '''
    count = 1
    while True:
        print('*****User Login*****')
        user = input('Please enter account(input n Return to upper level):')
        if user.upper() == 'N': return
        pwd = input('Please input a password:')
        with open('freeze',mode='r',encoding='utf-8') as f:   # Open frozen file,Determine whether the user name entered by the user is in the frozen file
            content = f.read().strip().split('|')
            if user in content:
                print('This user is frozen,Please re-enter your account')
                continue
        flag = False
        new_name = False
        with open('userinfo',mode='r',encoding='utf-8') as f:
            for line in f:
                a,b  = line.strip().split('|')
                if user == a:      # Determine whether the user name exists
                    new_name = True
                if a == user and b == get_md5(user,pwd):  # Determine whether the account password is correct
                    flag = True
                    print('\033[1;34m Login successfully\033[0m')
                    exit()
        if not new_name:
            print('Account does not exist, please re-enter')
            continue
        if not flag:
            print('Account or password error,Please re-enter')
            count += 1
            if count == 4:
                print('Wrong three times,Has been frozen')
                with open('freeze',mode='w',encoding='utf-8') as f:  # Write the wrong user name three times to the frozen file
                    f.write(user+ '\n')
                    return

def run():
    '''
    //Program entry
    :return:
    '''
    info = {'1':register,'2':login}
    while True:
        print('''Management interface
        1.User registration
        2.User Login''')
        choice = input('Please select service(input N Sign out):')
        if choice.upper() == 'N': break
        data = info.get(choice)
        if not data:
            print('Incorrect input,Please re-enter')
            continue
        data()


if __name__ == '__main__':
    run()

Tags: Python encoding

Posted on Wed, 06 Nov 2019 09:48:33 -0800 by monkeytooth