


Detailed explanation of the specific steps for writing a login window
Ideas:
1. Reference model. For this assignment, I referred to the login authentication process of Linux and the locking rules such as online banking, Alipay, etc.;
1) The authentication process refers to Linux login: after you enter the username and password, verify whether the username exists and whether the user is locked, and then verify whether the password is correct. If the verification fails, it will only tell you that the verification failed, not Tell you whether the user name or password is wrong, which increases the difficulty of brute force cracking;
2) Regarding the counting and locking of incorrect times, I refer to This is what banks and Alipay do, that is, they only care about how many times you entered incorrectly, and don’t care how many times you entered incorrectly. That is to say, if you enter incorrectly twice, the third time If you enter correctly, the previous count is not cleared, that is, you entered 1,000 times today and lost 997 times correctly, but only entered incorrectly 3 times. Sorry, you still have to lock it. And the three incorrect entries do not have to be consecutive. It will be locked after three incorrect inputs three times.
## 2. Regarding the saving ofcount and status, I consider persisting it through files . The accumulation and locking of each incorrect number of inputs are saved. in the file, although this increases the file operation, it ensures the reliability of the program. In this way, the program exits and the count is still valid. The file type is as follows:
alex sb lock 3 Tom 666 unlock 0 geng 888 unlock 2
##The code is as follows:
with open(filename,) key,values lines = print( f.write( .join(lines) + values.insert( f.write( .join(values) + # messages = {:[,,],:[,,],:[,, # write_file(messages, __name__ == active = username = input( users_dict = {} #用户字典,用于存放用户的信息,键-用户名,值- with open(,) lines = line user_list = users_dict[user_list[]] = user_list[ username users_dict[username][] == print( (users_dict[username][]) < user_pwd = input( users_dict[username][] == print( (users_dict[username][]) != print( % ( - (users_dict[username][ users_dict[username][] = (users_dict[username][]) + print( users_dict[username][] = users_dict[username][] = write_file(users_dict, elif username == print()
运行结果如下:
请输入你的用户名(输入quit退出):tom
请输入密码:22
还有2次机会,用户将被锁定!
请输入密码:22
还有1次机会,用户将被锁定!
请输入密码:22
对不起,您输入的次数过多,你的用户名已经被锁定,请联系管理员!
请输入你的用户名(输入quit退出):alex
您输入的用户名已经锁定,请联系管理员!
Code description:
1. Two contents not covered in today’s course are used here: dictionaries and functions to convert user information into dictionaries The reason for saving in this way is to determine whether the user name exists. When reading the file, the user name is directly used as a key of the dictionary, and other information is used as the value of the dictionary in one-to-one correspondence with the key. In this way, only the user name is needed to determine whether the user name exists. Just use the in member operator to judge;
2. The 61st and 62nd lines of the code complete the process of turning the file into a dictionary.
# Original Zhang Xiaoyu code example:
#!/usr/bin/env python3 # coding:utf-8'''Created on: 2015年12月29日 @author: 张晓宇 Email: 61411916@qq.com Version: 1.0Description: 输入用户名密码,认证成功显示欢迎信息,认证失败,输错三次后锁定 Help:'''import os # 定义用户信息写入函数,用于把用户信息写回文件 def write_to_account_file(accounts,account_file_path):"""accounts是一个用户信息字典,目的是把变更过的信息写入文件中"""account_file = open(account_file_path,"w")for key,val in accounts.items(): line = [] line.append(key) line.extend(val) print(" ".join(line)) #字符串与列表拼接,目的是实现列表中每个元素加空格生成一个字符串如a b c d格式 account_file.write(" ".join(line) + "\n") #往文件中添加信息 account_file.close() #直接打开文件的时候一定要记得关闭文件,以免文件休息丢失if __name__ == '__main__':''' @parameters: account_file_path:账户文件 password_col_num:账户文件中密码所在的列(从0开始) status_col_num:账户文件中账户状态所在的列(从0开始) error_count_num:账户文件中输入错误次数所在的列(从0开始) app_info:系统信息,用户启动应用后的输出 welcome_msg:用户成功登录后的信息''' account_file_path = 'account.db'password_col_num = 1status_col_num = 2error_count_num = 3app_info = ''' +---------------------------------+ | Welcome to gcx system | | Version:2.0 | | Author:zhuzhu | +---------------------------------+''' welcome_msg = "Welcome %s,authentication is successful!"if os.path.exists(account_file_path): #os.path.exists()判断文件是否存在,返回布尔值 # 判断账户文件是否存在 account_file = open(account_file_path,"r")else: #文件不存在,看系统是否有误 print("Error:Account file 'account.db' is not exit,please check!") exit(1) #读账户文件 accounts = {}for line in account_file.readlines(): #按行读取文件 account = line.strip().split() #清楚空格后进行分列,生成一个列表 accounts[account[0]] = account[1:]"""把列表中的第一个元素(用户名)当做键,用户的其他信息当做值,组成键值对存放在字典中""""""account[0]键,account[1:]是值"""account_file.close() #关闭文件 flag = Truewhile flag: print(app_info) #输入用户名 username = input("Username(Enter quit to exit): ").strip() #判断用户是否输入的为quitif username == "quit": #是则退出循环,程序结束breakpassword = input("Password: ").strip() #判断用户名是否存在if username not in accounts.keys(): #不存在提示错误信息并退出当前循环让用户重新输入 print("Error:Username or Password it is error!")continue #结束本次循环,让用户再次输入用户名和密码,继续执行下一次循环if accounts[username][status_col_num - 1] == "lock": #如果被锁定退出当前循环让用户重新输入 print("Error:Account is locked.Please contact the administrator!")continue #跳过本次循环,让用户重新输入进行验证,这个可以避免很多缩进,能够继续执行下一次 #判断用户密码是否正确if password == accounts[username][password_col_num - 1]: #正确显示欢迎信息 print(welcome_msg %username)breakelse: #用户密码不正确的情况 #提示用户名或密码错误 print("Error:Username or Password it is error!") #输入错误次数加1 accounts[username][error_count_num - 1] = str(int(accounts[username][error_count_num - 1]) + 1) #判断是否已经达3次if int(accounts[username][error_count_num - 1 ]) == 3: #如果输入错误达到3次 #提示账户将被锁定 print("Error: This account will be locked,Please contact the administrator!System will be exit!") #将用户状态改为lock并写入文件 accounts[username][status_col_num - 1] == "lock"write_to_account_file(accounts,account_file_path)breakwrite_to_account_file(accounts,account_file_path)
Highlights in the above code:
(1) continue: End this loop. The code after continue will not be executed to achieve the purpose of allowing the user to input again. Many The web page allows users to keep typing until the user either chooses to register or close the web page;
(2) The document is relatively standardized and has explanations;
(3) The file is read line by line, and then written to the file line by line. The knowledge of lists and dictionaries must be used in the modification process;
(4) " ".join(list) splicing of strings and lists to generate new strings;
(5) split() string splitting; extend() between lists splicing.
The above is the detailed content of Detailed explanation of the specific steps for writing a login window. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When you log in to someone else's steam account on your computer, and that other person's account happens to have wallpaper software, steam will automatically download the wallpapers subscribed to the other person's account after switching back to your own account. Users can solve this problem by turning off steam cloud synchronization. What to do if wallpaperengine downloads other people's wallpapers after logging into another account 1. Log in to your own steam account, find cloud synchronization in settings, and turn off steam cloud synchronization. 2. Log in to someone else's Steam account you logged in before, open the Wallpaper Creative Workshop, find the subscription content, and then cancel all subscriptions. (In case you cannot find the wallpaper in the future, you can collect it first and then cancel the subscription) 3. Switch back to your own steam

With the rapid development of social media, Xiaohongshu has become a popular platform for many young people to share their lives and explore new products. During use, sometimes users may encounter difficulties logging into previous accounts. This article will discuss in detail how to solve the problem of logging into the old account on Xiaohongshu, and how to deal with the possibility of losing the original account after changing the binding. 1. How to log in to Xiaohongshu’s previous account? 1. Retrieve password and log in. If you do not log in to Xiaohongshu for a long time, your account may be recycled by the system. In order to restore access rights, you can try to log in to your account again by retrieving your password. The operation steps are as follows: (1) Open the Xiaohongshu App or official website and click the "Login" button. (2) Select "Retrieve Password". (3) Enter the mobile phone number you used when registering your account

Thousands of ghosts screamed in the mountains and fields, and the sound of the exchange of weapons disappeared. The ghost generals who rushed over the mountains, with fighting spirit raging in their hearts, used the fire as their trumpet to lead hundreds of ghosts to charge into the battle. [Blazing Flame Bairen·Ibaraki Doji Collection Skin is now online] The ghost horns are blazing with flames, the gilt eyes are bursting with unruly fighting spirit, and the white jade armor pieces decorate the shirt, showing the unruly and wild momentum of the great demon. On the snow-white fluttering sleeves, red flames clung to and intertwined, and gold patterns were imprinted on them, igniting a crimson and magical color. The will-o'-the-wisps formed by the condensed demon power roared, and the fierce flames shook the mountains. Demons and ghosts who have returned from purgatory, let's punish the intruders together. [Exclusive dynamic avatar frame·Blazing Flame Bailian] [Exclusive illustration·Firework General Soul] [Biography Appreciation] [How to obtain] Ibaraki Doji’s collection skin·Blazing Flame Bailian will be available in the skin store after maintenance on December 28.

The solution to the Discuz background login problem is revealed. Specific code examples are needed. With the rapid development of the Internet, website construction has become more and more common, and Discuz, as a commonly used forum website building system, has been favored by many webmasters. However, precisely because of its powerful functions, sometimes we encounter some problems when using Discuz, such as background login problems. Today, we will reveal the solution to the Discuz background login problem and provide specific code examples. We hope to help those in need.

How to set Google Chrome to open a new window every time? Vicious users like to use Google Chrome for work or study. This browser is safe, fast, and convenient. Different users have different preferences for using browsers. Some users like to open Google Chrome as a new window to facilitate quick searches. , so how to set it up. Next, the editor will bring you a tutorial on setting up a new window every time you open Google Chrome. Friends who are interested can come and learn it. Tutorial on setting up a new window every time Google Chrome opens 1. Double-click Google Chrome on your computer desktop to open it, then click on the [three dots] icon in the upper right corner. 2. Find the [Settings] option and enter the page (as shown in the picture). 3. Go to Google Chrome

Recently, some friends have asked me how to log in to the Kuaishou computer version. Here is the login method for the Kuaishou computer version. Friends who need it can come and learn more. Step 1: First, search Kuaishou official website on Baidu on your computer’s browser. Step 2: Select the first item in the search results list. Step 3: After entering the main page of Kuaishou official website, click on the video option. Step 4: Click on the user avatar in the upper right corner. Step 5: Click the QR code to log in in the pop-up login menu. Step 6: Then open Kuaishou on your phone and click on the icon in the upper left corner. Step 7: Click on the QR code logo. Step 8: After clicking the scan icon in the upper right corner of the My QR code interface, scan the QR code on your computer. Step 9: Finally log in to the computer version of Kuaishou

How to log in to two devices with Quark? Quark Browser supports logging into two devices at the same time, but most friends don’t know how to log in to two devices with Quark Browser. Next, the editor brings users Quark to log in to two devices. Method graphic tutorials, interested users come and take a look! Quark Browser usage tutorial Quark how to log in to two devices 1. First open the Quark Browser APP and click [Quark Network Disk] on the main page; 2. Then enter the Quark Network Disk interface and select the [My Backup] service function; 3. Finally, select [Switch Device] to log in to two new devices.

Baidu Netdisk can not only store various software resources, but also share them with others. It supports multi-terminal synchronization. If your computer does not have a client downloaded, you can choose to enter the web version. So how to log in to Baidu Netdisk web version? Let’s take a look at the detailed introduction. Baidu Netdisk web version login entrance: https://pan.baidu.com (copy the link to open in the browser) Software introduction 1. Sharing Provides file sharing function, users can organize files and share them with friends in need. 2. Cloud: It does not take up too much memory. Most files are saved in the cloud, effectively saving computer space. 3. Photo album: Supports the cloud photo album function, import photos to the cloud disk, and then organize them for everyone to view.
