Python Basic Learning Landing Job

Source: Internet
Author: User

Blog addr:http://www.cnblogs.com/uyahs/p/7667783.html
Program Summary:
The program runs in Python3. In the X environment
The program has a total of two files that need to be placed in the same directory.
The main program is zuoye_login.py
Zuoye_login.txt is the file to store account information, if you need to add the account password format as follows:
User:password;0

After the program is run, the account password will be entered, and the program will find and compare information from the password text, if correct, display the success information.
If the failure of the Account record 1 times the wrong login, more than three error login, then lock the account for 100 seconds.

#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Author:xtian
‘‘‘
This is a login module, the account password is saved in the same directory under the Zuoye_login.txt file
Zoye_login.txt file format such as: root;server;0 is the account number, password, login failure, three values separated by a semicolon
After running this program, if the password is wrong, the account will be logged an error, more than three times to lock the account for 100 seconds.
‘‘‘



Import Getpass
Import time
Login_status = 0 #这个值为登陆状态, if equals 1 indicates successful login verification
While True:
f = open (' Zuoye_login.txt ', ' r+ ')
Text = F.read (). Split (' \ n ')
F.close ()
Guess_user = input ("User:")
GUESS_PASSWD = Getpass.getpass ("Password:")
For j in Range (len (text)):
if Guess_user = = Text[j].split (';') [0]:
if Int (Text[j].split (';') [2]) > 2: #如果该账号密码登陆错误次数超过2
LocalTime = Time.localtime (Time.time ())
now_time = Int (time.mktime (localtime)) #获取现在的时间戳
unit_time = Int (now_time)-int (Text[j].split (';') [3]) #计算出当前时间和记录时间差
If Unit_time >: #将时间差与100比较, more than 100 can continue
TEXT[J] = "%s;%s;0"% (Text[j].split (';') [0],text[j].split (';') [1]) #将失败次数重置为0
New_text = ' \ n '. Join (text)
f = open (' Zuoye_login.txt ', ' W ')
F.writelines (New_text)
F.close ()
else: #时间差不足100秒, re-cycle by login failure
Wait_time = 100-unit_time #当前还需要等待的时间 (sec)
Print ("There is too many errors,please wait for%s second"% (Wait_time))
Input ("Please input ' ENTER ' to try Again")
Break
if guess_passwd = = Text[j].split (';') [1]: #判断密码是否正确
Login_status = 1 #将登陆状态更改为1, indicating successful login
Break
Else
If Text[j].split (';') [2]: #判断zuoye_login. txt file in which the number of failures in this account is not empty
failed_count = Int (Text[j].split (';') [2]) #获取该账号失败次数
Failed_count + = 1 #将该账号失败次数 +1
LocalTime = Time.localtime (Time.time ())
failed_login_time = Int (time.mktime (localtime)) #获取登陆失败时的时间戳 and deposit to file
TEXT[J] = "%s;%s;%s;%d"% (Text[j].split (';') [0],text[j].split (';') [1],failed_count,failed_login_time]
New_text = ' \ n '. Join (text)
f = open (' Zuoye_login.txt ', ' W ')
F.writelines (New_text)
F.close ()
Else: Add a value for the number of failures in #如果zuoye_login. txt file if the number of failures in this account is null
TEXT[J] = "%s;%s;%s;%s"% (Text[j].split (';') [0],text[j].split (';') [1],failed_count,failed_login_time]
New_text = ' \ n '. Join (text)
f = open (' Zuoye_login.txt ', ' W ')
F.writelines (New_text)
F.close ()
Else
Continue

if login_status = = 1: #判断是否登陆成功
Print ("Login success!")
Print ("Welcome", Guess_user)
Break
Else
Print ("Login failed!")



Python Basic Learning Landing Job

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.