User Login-determines that the entered password is incorrect three times and then the account is frozen. The user login password is incorrect.

Source: Internet
Author: User

User Login-determines that the entered password is incorrect three times and then the account is frozen. The user login password is incorrect.

'''
Create a user and lock file, and store the account and password of the user. This file is used for Logon judgment. The lock file is frozen three times after an account error occurs.
User Content format:
{'Name': 'hangsan ', 'passwd': '000000 '}
{'Name': 'lisi', 'passwd': '000000 '}
{'Name': 'hangw', 'passwd': '000000 '}
'''

Import OS
'''Try/try t''' is not used for this method '''

Def lock (_ name ):
''' Determine whether a user is frozen '''
If OS. path. getsize ("lock "):
With open ("lock", "r") as f:
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
If line ["name"] = _ name and line ["num"] = 3:
Return 0
Else:
Return-1
Else:
Return-1

Def clear_lock ():
'''When the program exits, the non-frozen account information will be deleted '''
List = []
With open ("lock", "r +") as f:
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
If line ["num"] = 3:
List. append (line)
F. truncate (0)
With open ("lock", "w") as f1:
For I in list:
F1.write (str (I) + '\ n ')

Def login (_ name, _ passwd ):
'''User login '''
With open ("user", "r") as f:
Flag = 0
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
If line ["name"] = _ name:
If line ["name"] = _ name and line ["passwd"] = _ passwd:
Return 0
Else:
Return 1
Return-1

Def write_lock (_ name ):
'''Write the wrong account into the lock file '''
# The file is not empty.
If OS. path. getsize ("lock "):
List = []
With open ("lock", "r +") as f:
Flag = 0
For line in f:
If line. strip () = "":
Continue
Line = eval (line)
# Determine whether an account exists
If line ["name"] = _ name:
Line ["num"] + = 1
List. append (line)
Else:
Dict2 = {}
Dict2 ["name"] = _ name
Dict2 ["num"] = 1
List. append (dict2)
With open ("lock", "w") as f1:
For I in list:
F1.write (str (I) + '\ n ')


# Writing Empty files directly
Else:
List1 = []
Dict1 = {}
Dict1 ["name"] = _ name
Dict1 ["num"] = 1
List1.append (str (dict1 ))
With open ("lock", "w") as f2:
For j in list1:
F2.write (str (j) + '\ n ')

Def main ():
Name = input ("User name :")
Res = lock (name)
If res = 0:
Print ("% s has been frozen, please contact the Administrator" % name)
Clear_lock ()
Exit ()
Passwd = input ("Password :")
Res1 = login (name, passwd)
If res1 = 0:
Print ("Welcome % s login 51CTO" % name)
Clear_lock ()
Exit ()
Elif res1 = 1:
Print ("Incorrect password ")
Write_lock (name)
Else:
Print ("account does not exist ")

Res2 = lock (name)
If res2 = 0:
Print ("% s has been frozen, please contact the Administrator" % name)
Clear_lock ()
Exit ()

While True:
Main ()



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.