脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python密码错误三次锁定(实例讲解)

python密码错误三次锁定(实例讲解)

2020-12-17 00:57smelond Python

下面小编就为大家分享一篇python密码错误三次锁定的实例讲解。具有很好的参考价值。希望对大家有所帮助。一起跟随小编过来看看吧

程序需求:

输入用户名,密码

认证成功显示欢迎信息

输入错误三次后锁定用户

流程图:

好像画的不咋地

python密码错误三次锁定(实例讲解)

查看代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# File_type:一个登录接口
# Author:smelond
import os
 
username = "smelond"#用户名
password = "qweqwe"#密码
counter = 0#计数器
 
#读取黑名单
file = os.path.exists("./user.txt")#检查当前目录是否有user.txt这个文件,如果有者输出True赋给file
if file == True:#判断是否有user.txt这个文件
 blacklist_file = open("user.txt", "r").read()#open()打开文件,并且用read()读取文件,然后赋给blacklist_file
 if blacklist_file == username:#检查文件里面的内容是否和我们的用户名相等
  print("Username lock. Please contact the administrator to remove the restrictions!!!")#输出错误提示
  exit()#退出程序
 
#登录接口
for i in range(3):
 counter += 1#对每次登录进行计数
 input_user = input("Please input username: ")
 input_pass = input("Please input password: ")
 if input_user == username and input_pass == password:
  print("Welcome login...")
  break
 else:
  print("ERROR Incorrect username or password!!!")
  continue
 
#写入黑名单
if counter == 3:#判断我是否输入错误三次
 print("The user name has been disabled")#提示信息
 blacklist_user = open("user.txt", "a")#以追加模式打开 (从 EOF 开始, 必要时创建新文件)
 blacklist_user.write("%s" % username)#将用户名写入黑名单
 blacklist_user.close()#使用open后一定要记得调用文件对象的close()方法

以上这篇python密码错误三次锁定(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/smelond/archive/2017/11/13/7828020.html

延伸 · 阅读

精彩推荐