Home  >  Article  >  Backend Development  >  How to write a login interface using python

How to write a login interface using python

高洛峰
高洛峰Original
2017-03-10 19:01:271812browse

This article uses python to write a login interface method

Requirements:

Writing a login interface

Enter the user name and password

After successful authentication Display the welcome message

Lock after three inputs

User information file

Blacklist file

Check in the blacklist and do not allow login

Username and password determination

Flow chart:

How to write a login interface using python

Code:

#!/usr/bin/env python
tries = 0
lockfile = open('account_lock.txt','r+',encoding='UTF-8')
tolockfile = open('account_lock.txt','a',encoding='UTF-8')
userfile = open('account.txt','r',encoding='utf-8')
 
def islock(account):
    for line in lockfile:
        line = line.strip('\n')
        if line == account:
            print('此账号已锁定')
            exit()
 
def inaccount(account):
    for line in userfile:
        col1_user,col2_pass = line.strip().split()
        if col1_user == account:
            passtries = 0
            while passtries < 3:
                pass1 = input(&#39;请输入密码:&#39;)
                if col2_pass == pass1:
                   print(&#39;欢迎使用&#39;)
                   exit()
                else:
                   passtries +=1
            else:
                tolockfile.write(&#39;%s\n&#39; %(account))
                print(&#39;账号已锁定!&#39;)
                exit()
 
        else:
            print(&#39;此账号不存在&#39;)
            break
 
while tries<3:
    account = input(&#39;请输入账号:&#39;)
    islock(account)
    inaccount(account)
    tries +=1
else:
    print(&#39;此用户真的不存在 88&#39;)
    exit()
 
lockfile.close()
userfile.close()
tolockfile.close()
 
 
 
# with open(&#39;account_lock.txt&#39;,&#39;a&#39;,encoding=&#39;UTF-8&#39;) as data:  
#     data.write("aaa")  
#此方法可以方式忘记文件close
#tolockfile = open(&#39;account_lock.txt&#39;,&#39;a&#39;,encoding=&#39;UTF-8&#39;)
#open文件后进行追加至最后一行


The above is the detailed content of How to write a login interface using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn