python用户登录a_用Python实现用户登录接口

项目需求描述:要求用户输入用户名和密码,认证成功后显示欢迎信息,如果连续输错三次密码则锁定该用户。

逻辑流程图:

wKiom1Qys9DAAR9AAAGWHQ5vSb4391.jpg

实现代码:#!/usr/bin/env python

import sys

account_file = 'account.txt'

lock_file = 'lock.txt'

# put accounts in a list

fh_account = open(account_file)

account_list = fh_account.readlines()

fh_account.close()

# every account has 3 chances of attempt at the beginning

retry_count = {}

for line in account_list:

line = line.split()

retry_count[line[0]] = 3

while True:

# put locked accounts in a list

fh_lock = open(lock_file)

lock_list = []

for i in fh_lock.readlines():

line = i.strip('\n')

lock_list.append(line)

fh_lock.close()

# handle the username and password empty issue

username = raw_input('Username: ').strip()

if len(username) == 0:

print '\033[31;1mUsername should not be empty !\033[0m'

continue

password = raw_input('Password: ').strip()

if len(password) == 0:

print '\033[31;1mPassword should not be empty !\033[0m'

continue

# authentication part

if username in lock_list:

print "\033[31;1mSorry, '%s' is locked already !\033[0m" % username

continue

if not retry_count.has_key(username): # inexistent account

retry_count[username] = 3

for line in account_list:

line = line.split()

if username == line[0] and password == line[1]: # authentication pass

retry_count[username] = 3 # reset retry times for this account

sys.exit('\033[32;1mWelcome %s login my system !\033[0m' % username)

else: # authentication failed

print '\033[31;1mWrong username or password !\033[0m'

retry_count[username] -= 1

if retry_count[username] > 0:

print '\033[31;1mYou have %d more chances !\033[0m' % retry_count[username]

else: # no more chance of attempt

fh = open(lock_file, 'a')

fh.write('%s\n' % username)

fh.close()

print "\033[31;1mSorry, '%s' is locked !\033[0m" % username

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值