python实现登录验证(循环练习)

练习python的while循环控制,模拟登录验证,登录失败三次会锁定账户。
login_validate.py
##!/usr/bin/env python
# -*- coding: UTF-8 -*-
#author:lonerangerr
count = 0
retry_limit = 3
while count < retry_limit:
    username = raw_input("Enter your username:")
    with open('lockfile','r') as f:
        for i in f.readlines():
            if username == i.split()[0]:
                print "Sorry,%s:your account is locked,process will be quit..."%username
         f.close() exit() password
= raw_input("Enter your passwd:") match = False with open('userinfo','r') as f: for line in f.readlines(): user,passwd = line.strip('\n').split() if username == user and password == passwd: #print "yyyyyyyyyyyyyyyyyyes..." pass match = True break if match ==False: print "Unmatched!!! u still have %s times to retry..."%(2-count) count += 1 else: print "Hello,%s: Welcome to login..."%username break f.close() else: print "Sorry,%s:your account will be locked..."%username with open('lockfile','a') as f: f.write(username) f.write('\n') f.close()

运行此程序需要的另外两个文件(三个文件放在同一个目录下)lockfile(被锁定用户)、userinfo(用户账户、密码信息),格式如下

lockfile

locked_user1
locked_user2
locked_user3

userinfo

user1    111
user2    111
user3    111

  

posted @ 2017-04-09 13:58  lonerangerr  阅读(656)  评论(0编辑  收藏  举报