This article mainly gives you detailed information about how to use python to log on to the system. if you are interested, you can refer to the example in this article to share the python implementation code of the user logon system, for your reference, the specific content is as follows:
Note:
1. run the program using python3. Enter 1 or 2 as prompted to log on to or exit the program.
2. after you enter the user name, if the user name is locked or the user name is invalid, the program will exit. make sure that the user name is entered correctly.
3. enter the correct user name and password. If the user name is correct, the password is mistakenly entered three times in a row, and the user is locked. logon to the system is prohibited.
#!/usr/local/env python#coding:utf-8#Auto:Panwenbin#function:lock_file=open('file_lock.txt','r+')user_pass=open('username_file.txt','r+')count=0
Cmd = input (''' 1: log on to the system. 2: log out of the system. Please enter your operation: ''') if cmd. isdigit () and int (cmd) = 2: exit () elif cmd. isdigit () and int (cmd) = 1: while count <3: # as long as the number of retries does not exceed 3, the username = input is continuously cyclically ('Enter your username :') for I in lock_file.readlines (): # determine whether the user name is in the lock file I = I. split () if username in I [0]: print ('Sorry % s locked '% username) exit ()
Match = False for j in user_pass.readlines (): user, password = j. strip ('\ n '). split () # remove the redundant \ n of each line and divide this line into two columns by space. the values are respectively user and passwd variables if username = user: # determine whether the input user has passwd = input ('Enter password: ') if password = passwd: print ('user name and password are correct') match = True break elif password! = Passwd: # When the user name is correct, determine whether the entered password is correct for I in range (2): passwd = input ('wrong password, please re-enter the password :') if password = passwd: print ('user name and password are correct ') match = True break
If username! = User: # determine whether the user does not exist print ('The user name you entered is not saved, the program has exited ') exit () elif match = False: # if the match is False, it indicates that the above loop does not match the user name and password print ('password and user name do not match, more than three attempts, the user is locked ') lock_file.write ('% s \ n' % username) lock_file.close () user_pass.close () exit () elif match = True: print ('logon successful') break else: print ('invalid option, program exited ')
The above is all the content of this article. I hope it will help you learn python programming.
For more articles about python user logon to the system, please follow the PHP Chinese network!