python密码验证_Python用户密码登陆匹配验证(文件处理)

#需求

编写登陆接口

- 输入用户名密码

- 输错三次后锁定账户

- 认证成功后显示欢迎信息

#脚本目录[root@localhost opt]# tree

.

├── account_lock.txt

├── accounts.txt

└── login.py

0 directories, 3 files

#脚本文件#!/usr/bin/env python

#_*_ coding:utf-8 _*_

import sys

retry_limit = 3

retry_count = 0

account_file = "accounts.txt"

lock_file = "account_lock.txt"

#三次验证循环

while retry_count 

#让用户输入用户名

username = raw_input("\033[31m Username: \033[0m")

lock_check = file(lock_file)

for line in lock_check.readlines():

if username in line:

sys.exit()

#让用户输入密码

password = raw_input("\033[31m Password: \033[0m")

#获得account文件的用户和密码

f = file(account_file,"rb")

match_flag = False

for line in f.readlines():

user,passwd = line.strip("\n").split()

#对用户输入的用户密码进行匹配验证

if username == user and password == passwd:

print "Match!", username

match_flag = True

break

f.close()

#判断有没有匹配成功,不成功则进行下一次循环,成功则输入欢迎信息

if match_flag == False:

print "User unmatched"

retry_count += 1

else:

print "Welcome login wsyht Lerning system!"

sys.exit()

else:

#三次循环完后开始锁账户

print "Your account is locked!"

f = file(lock_file,"ab")

f.write(username)

f.close()

#查看账户文件[root@localhost opt]# cat accounts.txt

wsyht wsyht

#查看锁文件[root@localhost opt]# cat account_lock.txt

[root@localhost opt]#

#执行脚本不匹配显示[root@localhost opt]# python login.py

Username: jenkins

Password: jenkins

User unmatched

Username: jenkins

Password: jenkins

User unmatched

Username: jenkins

Password: jenkins

User unmatched

Your account is locked!

#查看锁文件[root@localhost opt]# cat account_lock.txt

jenkins[root@localhost opt]#

#执行脚本匹配显示[root@localhost opt]# python login.py

Username: wsyht

Password: wsyht

Match! wsyht

Welcome login wsyht Lerning system

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值