Python Impersonation User Login code

Source: Internet
Author: User

Requirements: Enter the user name, determine whether the user is locked, lock the exit, otherwise enter the password authentication, enter three times the wrong password after the user is locked.

Userlist.txt, use ":" to separate the user name, password, status code:

[[Email protected] ~] #  Zhang San: 123: 0 John Doe:456: 0admin:123:0qqq: 123:0

Code:

1 #!/usr/bin/env python2 #-*-coding=utf-8-*-3 __author__='GMK'4 5 ImportSYS6 ImportFileinput7 ImportGetpass8 9 #Open User list, read onlyTenFileInfo = File ('Userlist.txt','R') OneUser_file =Fileinfo.readlines () A fileinfo.close () -  - #definition Dictionary {' Zhang San ': [' 123 ', ' 1 '],...} theUserDict = {} -  forLineinchUser_file: -     #userinfo = [' Zhang San ', ' 123 ', ' 0 '] -UserInfo = Line.strip (). Split (":") +     #userdict[' Zhang san '] = [] -Userdict[userinfo[0]] = [userinfo[1],userinfo[2]] +  A  at #Verify user name - defVerify_user (inputname): -     #user name is empty -     ifInputName = ="': -InputName = Raw_input ('user name cannot be empty, please enter user name:'). Strip () - Verify_user (InputName) in     #user name does not exist -     elifInputName not inchuserdict: toInputName = Raw_input ('user does not exist, please re-enter the user name:'). Strip () + Verify_user (InputName) -     Else: the         #user name is correct, determine whether to lock *         ifUSERDICT[INPUTNAME][1] = ='1': $             Print 'This user is locked, please contact the administrator! 'Panax Notoginseng sys.exit () -         Else: the         #user not locked, enter password to judge +             Print 'user name is correct! ' A verify_pwd (InputName) the  +  - #Verify Password $ defverify_pwd (okname): $Pwdinfo = Getpass.getpass ('Please enter your password:'). Strip () -i = 2 -      whileTrue: the         ifPwdinfo = =Userdict[okname][0]: -             Print '%s Welcome back! '%OknameWuyi sys.exit () the         elifPwdinfo = ="': -Pwdinfo = Getpass.getpass ('The password cannot be empty, please enter the password:'). Strip () Wu         Else: -             ifi >0: AboutPwdinfo = Getpass.getpass ("password error, you have%d chances, please re-enter the password:"%i) $I-= 1 -             Else: - update_file (okname) -                 Print 'password input more than three times, the user is locked! ' A sys.exit () +  the #update file contents, lock users - defUpdate_file (okname): $     #the old string admin:123456:0 theOld_str =':'. Join ([Okname,userdict[okname][0],str (userdict[okname][1])]) the     #Setting the Lock identity theUSERDICT[OKNAME][1] = 1 the     #the new string admin:123456:1 -New_str =':'. Join ([Okname,userdict[okname][0],str (userdict[okname][1])]) in  the     #Replace user state information the      forLineinchFileinput.input ('Userlist.txt', inplace=1): About         PrintLine.strip (). Replace (OLD_STR,NEW_STR) the fileinput.close () the  the  +User_input = Raw_input ('Welcome to the employee backend system! Please enter user name:'). Strip () -Verify_user (User_input)
View Code

Operation Result:

1[[Email protected] ~]#python login.py2 Welcome to the employee backend system! Please enter user name:3 user name cannot be empty, please enter user name: Qweqwerqwer4 user does not exist, please re-enter the user name: QQQ5 user name is correct! 6 Please enter your password:7 The password cannot be empty, please enter the password:8Password error, you have 2Second Chance, please re-enter the password:9 The password cannot be empty, please enter the password:Ten The password cannot be empty, please enter the password: OnePassword error, you have 1Second Chance, please re-enter the password: A password input more than three times, the user is locked!  -[[Email protected] ~]#python login.py - Welcome to the employee backend system! Please enter user name: QQQ the This user is locked, please contact the administrator!  -[[Email protected] ~]#python login.py - Welcome to the employee backend system! Please enter user name: admin - user name is correct!  + Please enter your password: -Admin Welcome back!

Userlist.txt results:

1 Cat  2 Zhang San:123:03 John Doe:456:04 admin: 123:05 QQQ:123:1

Python Impersonation User Login code

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.