Python 用户登录练习

本文介绍了一个简单的用户登录系统实现,该系统通过匹配预设的用户名和密码来验证用户身份,并在连续三次登录失败后自动锁定账户以增加安全性。
#!/usr/bin/env python
#_*_ coding:utf-8 _*_
import os 
import sys
count=3
retry_count=0
account_file='/home/hello/login.txt'
lock_file='/home/hello/lock.txt'
while retry_count < count:
    username=raw_input('input the username: ')
    lock_check=file(lock_file)
    for line in lock_check.readlines():
line=line.split()
        if username == line[0]:
   sys.exit('user is locked')
#            print '%s is locked',username
#            break
    password=raw_input('input the password: ')
    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 'login successful'
            match_flag=True
            break
    f.close()
    if match_flag == True:
print 'enjoy your system'
        break
    elif match_flag == False:
        print 'User is not matched'
        retry_count +=1
#    else:
#        print 'enjoy your system'
else:
    print 'your account is locked'
    f = file(lock_file,'ab')
    f.write(username)
    f.close()
           
note:登录系统,输入用户名和密码,如果三次登录失败的话,将用户进行锁定
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值