Python simulates user login verification, and python simulates User Login

Source: Internet
Author: User

Python simulates user login verification, and python simulates User Login

The examples in this article share with you the specific code of the Python simulated User Logon verification for your reference. The details are as follows:

1. Features

This program simulates the user login verification process to achieve user name input, blacklist detection, user validity identification, password input and verification. The user successfully entered the correct password within three times. The User Failed to log on three times consecutively and the user name was recorded in the blacklist. the user in the blacklist is locked and cannot log on again.

2. Implementation Method

This program is written in the python language. It breaks down various tasks and defines corresponding functions for processing, so that the program structure is clear and easy to maintain. Mainly compiled four functions:

Login () # login function, mainly used for user name detection and password verification
Inquire_blacklist (name) # query whether the user name is in the blacklist
Inquire_userlist (name) # query whether the user name is in the user name list and return the password or None
Add_blacklist (name) # Add the user name to the blacklist File
Function call framework: Run login () --> enter the user name --> call inquire_blacklist (name) for blacklist detection --> call inquire_userlist (name) to identify the validity of the user name. If it is valid, the user password is returned, then compare and verify with the entered password --> if you enter the wrong password three times in a row, call add_blacklist (name) to add the user to the blacklist.

3. Flowchart

4. Code

# Author: Byron Liimport osBASE_DIR = OS. path. dirname (_ file _) indicates the directory named userlistpolic'userlist.txt ', which stores the user name and password blacklistpolic'blacklist.txt' # access the user's blacklist file userlist_path = OS. path. join (BASE_DIR, userlist) # register the user name for a single file path blacklist_path = OS. path. join (BASE_DIR, blacklist) # blacklist file path # --------------------------- login function (main function) ---------------------------- def login (): # login function username = input ("Please input username :") while (True): if in Quire_blacklist (username): # query whether the input user name is printed in the blacklist ("sorry, this user name \" % s \ "has been locked, login prohibited! "% Username) return False else: user_password = inquire_userlist (username) # query whether the input user name exists in the registration user name list. if yes, the system returns the user password if user_password: for I in range (3): password = input ("enter the password:") if I = 0 else input ("the password is incorrect. Please enter the password again :") if password = user_password: print ('Welcome % s login! '% Username) return True else: print ("You have entered the wrong password three times in a row. The user name will be locked and cannot be logged on again! ") Add_blacklist (username) # Add the user name to the blacklist file return False else: username = input ('invalid username, please enter it again :') # blacklist # ----------------- query blacklist function ------------------------------- def inquire_blacklist (name): # query whether the user name is in the blacklist with open (blacklist_path, 'R') as f: for line in f: if name = line. strip (): return True return False #----------------------------------------------- -------------- # ------------------------- Query the username function ------------------------ def inquire_userlist (name): # query whether the username is in the registration username list with open (userlist_path, 'R') as f: [username, password] = line. split () if name = username: return password return None # blacklist # ------------------------------- add blacklist function -------------------- def add_blacklist (name): # Add username Blacklist file with open (blacklist_path, 'A') as f: f. write (str (name) + '\ n') return True # ------------------------------------------------------------------------ if login (): # execute the login function print (' login successful! '. Center (47,' * ') else: print ('login failed! '. Center (47 ,'*'))

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.