Home  >  Article  >  Backend Development  >  Use python to write login interface methods

Use python to write login interface methods

高洛峰
高洛峰Original
2017-03-22 13:33:172330browse

Python writing login interface

1. Requirements

Writing login interface:

1. Enter username and password to log in

2. Enter wrong three times Lock the account

3. The next time you log in, it will still be the last account. It will prompt you to lock it. Log out directly (using file reading and writing)

4. After successful login, it will show successful login

2. Requirement flow chart

使用python编写登录接口方法

3. Code example

例1:
 
#!/bin/bash/env python
#_*_ coding:utf-8 _*_
#python version:3.6
 
'''
编写登录接口:
    1.输入用户名和密码登录
    2.输错三次锁定账户
    3.下次登录还是上次的账户,提示锁定,直接退出(用到文件读写)
    4.成功 后显示登录成功
'''
#lock定义为锁定文件
lock = "E:/Python_learn/day1/lock"
#account定义为账户文件
account = "E:/Python_learn/day1/account"
#计数器
count = 0
#标识器
flag = 1
#定义锁定用户列表为空
lock_user = []
 
#打开锁定文件,并读取锁定账户
f1 = open(lock,'r')
lock_file = f1.readlines()
f1.close()
 
#循环锁定账户,将账户追加到lock_user列表中
for i in lock_file:
    i = i.strip('\n')
    lock_user.append(i)
#打开账户文件,并读取用户和密码
f2 = open(account,'r')
account_file = f2.readlines()
f2.close()
 
while True:
    name = input("input your name:")
    passwd = input("input your password:")
    #如果输入的账户在锁定用户列表中,退出循环;
    if name in lock_user:
        print("user is lock!")
        break
    else:
        #否则计数器加count+1
        count += 1
        #如果count大于2,也就是输错三次
        if count > 2:
            print("错误三次")
            #将账户添加到锁定账户中
            with open(lock,'a') as f:
                f.write("\n" + name)
            break
        #如果count小于2,
        else:
            #循环输入的用户名和密码,是否和账户文件里边的一样
            for i in account_file:
                n1,p1 = i.strip().split()
                if name == n1 and passwd == p1:
                    print("welcome login!!")
                    #如果账户密码一样,flag标识为True
                    flag = True
                #如果输入账户密码和文件存储的不一样,则跳出本次循环
                else:
                    #跳出本次循环
                    continue
        #如果flag标识为True,退出整个循环
        if flag is True:
            break
 
例2:
 
#!/bin/bash/env python
#_*_ coding:utf-8 _*_
#python version:3.6
 
lock = "E:/Python_learn/day1/lock"
account = "E:/Python_learn/day1/account"
 
count = 0
flag = 1
lock_user = []
 
f1 = open(lock,'r')
lock_file = f1.readlines()
f1.close()
for i in lock_file:
    i = i.strip('\n')
    lock_user.append(i)
 
f2 = open(account,'r')
account_file = f2.readlines()
f2.close()
 
while True:
    name = input("input your name:")
    passwd = input("input your password:")
    if name in lock_user:
        print("user is lock!")
        break
    else:
        count += 1
        for i in account_file:
            n1, p1 = i.strip().split()
            if name == n1 and passwd == p1:
                print("welcome login!!")
                flag = True
            else:
                continue
    if flag is True:
        break
    else:
        if count > 2:
            print("错误三次")
            with open(lock,'a') as f:
                f.write("\n" + name)
                break


The above is the detailed content of Use python to write login interface methods. 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