用户登陆接口

需求:

1、输入用户名、密码

2、认证通过欢迎

3、三次失败锁定用户

一:

#!/usr/bin/env python
import os        
#导入模块
account_file = 'account.txt'    #用户文件
lock_file = 'lock.txt'          #锁定用户文件
for i in range(3):
        username = raw_input("username:").strip()    #消除空格,包括\n\r\t
        password = raw_input("password:").strip()
        if len(username) !=0 and len(password) !=0:  #判定非空
                f = file(account_file)
                loginSuccess = False   #置标志位
                for line in f.readlines():
                    line = line.split()     #切
                    if username == line[0] and password ==line[1]:
                        # user and passwd are correct
                        print "Welcome %s login my system" % username
                        loginSuccess = True
                        break
                if loginSuccess is True: # login success
                        break
        else:
                continue
else:
        f = file(lock_file)
        f.write('%s\n' % username)  
#写入锁定文件
        f.close()

二:

#!/usr/bin/env python
import os
account_file = 'account.txt'
lock_file = 'lock.txt'
#put account in a list
f = file(account_file)
acount_list = f.readlines()
f.close()

while True:
    #put locked user into a lock list
    f = file(lock_file)
    lock_list = []
    for i in f.readlines():
        line = i.strip('\n')     #rm \n
        lock_list.append(line )    #追加
    f.close()
    print lock_list

    loginSuccess = False
    username = raw_input('user:').strip()
    if username in lock_list:
        print "sorry"
        break
    for line in acount_list:
        line = line.split()
        if line[0] == username: #correct name
            for i in range(3):
                password = raw_input('passwd:').strip()
                if password == line[1]: # correct pd
                    print "Welcome %s login my system!@" % username
                    loginSuccess = True
                    break               
            else:
                f = file(lock_file,'a')
                f.write('%s\n' %username)
                f.close()
                print "Enter 3 times of wrong passwd,going to lock %s" % username    
        if loginSuccess == True:break # jump out of for top loop
    if loginSuccess == True:break # jump out of while loop

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值