Home > Backend Development > Python Tutorial > How to write a login interface?

How to write a login interface?

PHP中文网
Release: 2017-06-20 13:46:00
Original
1285 people have browsed it

Write login interface

1. Enter username and password

2. Display the welcome message after successful authentication

3. Lock after three incorrect entries

Idea: The requirements are When writing a login interface, there must be a module to store user information; to lock after three times, there must be a module to store locked user information; we know that there are two ways to save user information, one is database saving, and the other is file saving. , now we have only learned about file saving, so there are two files, one is the user information file and the other is the locked user information file.

To read information from a file, add information, and modify information.

readme:

(1) User enters user name;

(2) User Go to the locked file to verify whether the user is locked; if it is locked, ask the user to contact the administrator to solve it;

(3) If it is not locked, go to the user file to verify whether the user exists, and ask the user to enter the password. The welcome message is displayed after successful input; the user is locked after three incorrect inputs;

(4) If the user is not in the user file, it will prompt that the user is not registered and needs to register. Write a registration module to allow the user Register. After registration, add the user to the user list, and display the welcome message, preventing the user from typing, and automatically linking to the login status.

The flow chart is as follows:

##
active = None
#程序执行的标识符

def verification(username):"""验证用户是否锁定"""locked_users = []
    with open("locked_file") as locked_f:for line in locked_f:
            locked_user,locked_pwd = line.split(":")
            locked_users.append(locked_user)
        print(locked_users)if username in locked_users:
        print("对不起,你的用户名已经被锁定,请联系管理员!")else:
        active = Truereturn active

def is_registered(username):"""验证用户是否注册,注册就让用户登录,未注册让用户选择重新输入或者注册"""users = {}
    with open("active_file","r+") as f:for active_line in f:
            user,pwd = active_line.split(":")
            users[user] = pwdif username in users.keys():
        test_num = 0while test_num < 3:
            user_pwd = input("请输入您的密码:")if user_pwd == users[username]:
                print("welcome back,have a good time!")return Falseelse:
                test_num += 1else:
            #用户输入三次以上锁定用户,使用的是while...else...方法
            print("对不起,你输入的次数过多,你的用户已经被锁定,请联系管理员!")
            mes = "\n" + username + ":" + users[username]
            with open("locked_file","a") as f1:
                f1.write(mes)return Falseelse:
        print("您输入的用户名不存在,请按照下面的提示选择!")
        print("注册:请输入1\n登录:请输入2")
        num = input("请输入你的选择:")if num == "1":
            register_name = input("请输入你要注册的用户名:")while True:
                register_pwd = input("请输入你的密码:")
                register_pwd2 = input("请再次输入你的密码:")if register_pwd == register_pwd2:breakelse:
                    print("您输入的密码不对,请重新输入")
            message = "\n" + register_name + ":" + register_pwd
            print("Thank for your registing,have a good time!")
            with open("active_file","a") as f_obj:
                f_obj.write(message)return False

        elif num == "2":
            user_name = input("请重新输入你的用户名:")
            verification(user_name)if __name__ == "__main__":

    username = input("请输入你的用户名:")
    active = verification(username)while active:
        active = is_registered(username)
Copy after login

The above code realizes the verification and locking functions, and also realizes the registration function of new users, but there is a flaw, that is, when When the user is locked, it is not deleted from the current file. It is added to the file that locks the user information. Adding information to the file is very simple, but deleting a piece of information from the file is not very simple. It cannot be operated as you like with a list. You can only convert it first, and when operating files in the list, line breaks and other things are very disgusting. The above code will be converted as follows. Learning is a tedious thing, but it must be done perfectly. Next, I will refer to the code written by others, learn how others delete unnecessary information in the file, and then improve it. the code above.

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template