Python之编写登录接口

编写登陆接口

输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定

本文采用的文本在代码中已详细注释,流程图如下:在这里插入图片描述
代码如下:

#!/usr/bin/env python
# Author:Zhu dongdong
import os
import sys
#the article use file :
'''
username_Password.txt:
aaa 111
bbb 222
ccc 333
ddd 444
lock_username.txt:
aaa
ddd
'''
user_name = ""
password = ""
count = 0#the count is used for cycle(while)
count1 = 0#the count1 is used for judging
#check username is lock or not
def check_username_isLock():
    with open(r"E:/lock_username.txt")as file2:
        locklist = file2.readlines()
        for lock_name in locklist:
            username = lock_name.strip("\n")
            if username == user_name :
                print("the operation is too frequently")
                exit()
    return
#return 1 represent that username is exist and return 0 against
def check_username_isExist():
    x = 0
    with open(r"E:/userName_Password.txt")as file1:
        list = file1.readlines()
        for List_user_andName in list:
            (user, password1) = List_user_andName.strip("\n").split(" ")
            if user_name == user:
                x = 1
    return x
#check the password is right
def check_password_isok(count1):
    with open(r'E:/username_Password.txt')as file:
        list = file.readlines()
        for user in list:
            (username,passsword1) = user.strip("\n").split(" ")
            if user_name == username and password == passsword1:
                print("Welcome to login...")
                exit()
            elif user_name == username and password != passsword1:
                count1 = count1 + 1
                print("The input of password is error!")
    return count1
#write the username to 'lockfile'
def write_lockusername():
    file = open("E:lock_username.txt", "a")
    file.write('''\n{a}'''.format(a = user_name))
    return
while count == 0:
    user_name = input("please input the username:")
    check_username_isLock()
    ret = check_username_isExist()
    if ret == 1:#represent username is exist
        password = input("Please input the password:")
        break
    else :
        print("The username is error,Please try again.")
while count == 0:
    count1 = check_password_isok(count1)
    if count1 == 3:
        write_lockusername()
        exit(0)
    password = input("Please input the password:")









猜你喜欢

转载自blog.csdn.net/qq_40835367/article/details/82775543