python实现简单的登陆验证

本文介绍了一个基于Python实现的用户登录系统,包括密码验证、账户锁定和日志记录等功能。系统通过限制错误登录尝试次数并记录登录时间来保护用户账户的安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

登陆验证的流程图

#!/usr/bin/env python
# _*_ coding:utf-8 _*_

'''
login program
if your faild 3th,your will be locked

输入用户名密码
认证成功后显示欢迎信息
输错三次后锁定


用户输入一次密码就写入到日志中去。记录成功或者失败状态。
每次登陆的时候先去看user_lock文件,然后再看login.log 文件
如果login.log文件里在三分钟内登陆一直失败过,那么会算上次数。

user_lock 文件的格式
hansz   2015-10-27 21:31:26.159738

'''
import getpass,datetime, time, os

# 设置锁定时间和错误次数
global error_time
error_time = 3
global count
count = 3

# 定义登陆成功函数
# 打印欢迎信息,并且写入登陆时间到日志中去。
def login_succeed(user_name):
    print "欢迎您,尊敬的 %s" % user_name
    now_time = str(datetime.datetime.now())
    with open("session.log", "a") as f:
        login_info = "%s    login succeed in    %s \n" % (user_name, now_time.split(".")[0])
        f.write(login_info)
    return True

# 定义登陆失败函数
# 每次调用都会传入登陆次数,每次都会减1.
# 超过三次就写入到锁文件中去。
def login_failed(user_name, count):
    count = count - 1
    if count > 0:
        print "(密码输入错误,您还有%d次机会)" % count
        user_passwd = raw_input("请输入密码:")
        if_passwd_ok(user_name, user_passwd, count)
    else:
        print "错误次数超过三次,已锁定账号"
        os.system("sed -i '/%s/d' user_lock" % user_name)
        lock_time = str(datetime.datetime.now())
        with open("user_lock", "a")as f:
            lock_info = "%s %s\n" % (user_name, lock_time.split(".")[0])
            f.write(lock_info)
        with open("session.log", "a")as f:
            faild_info = "%s    are locked in   %s \n " % (user_name, lock_time.split(".")[0])
            f.write(faild_info)

# 把用户和登陆时间写入到锁用户文件中去
def user_lock(user_name, login_time):
    with open("user_lock", "a") as f:
        lock_user = "%s/n" % user_name
        f.write(lock_user)


# 把字符串格式的时间转换为datetime格式
def str_to_date(str_date):
    date_date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(str_date, "%Y-%m-%d %H:%M:%S")))
    return date_date

# 用于比较登陆时间和锁定时间
# 时间超过error_time之后 返回true
def date_compare(str_date_old, str_date_new):
    print "您现在登陆的时间是%s" % str_date_new
    date_date_new = str_to_date(str_date_new)
    date_date_old = str_to_date(str_date_old)
    diff = (date_date_new - date_date_old)
    diff_str = str(diff)
    # print "这个是时间差",int(diff_str.split(":")[0]),int(diff_str.split(":")[1])
    if int(diff_str.split(":")[0]) == 0:
        if int(diff_str.split(":")[1]) >= error_time:
            return True
        else:
            return False
    elif int(diff_str.split(":")[0]) > 0:
        return True
    else:
        return False


# 判断密码是否正确
def if_passwd_ok(user_name, user_passwd, count):
    if count > 0:
        # 从用户列表里面读取用户名和密码
        status0 = 0
        with open("user.list", 'r')as f:
            for i in f.readlines():
                # 如果用户名密码正确
                if i.split()[1] == user_name:
                    if i.split()[2] == user_passwd:
                        login_succeed(user_name)
                        status0 = 1
                        break
                    else:
                        login_failed(user_name, count)
                        status0 = 1
                        break
            if status0 == 0:
                print "没有这个用户"
    else:
        print "账号被锁定啦。"


# 从登陆日志查看是否近三分钟登陆过且登陆失败了
# 判断是否被锁定
def if_lock(user_name, user_passwd, login_time):
    # 读取user_lock 文件
    status1 = 0
    with open("user_lock", 'r') as f:
        content = f.readlines()
        if content == []:
            if_passwd_ok(user_name, user_passwd, count)
        else:
            for i in content:
                # 如果用户名存在在列表当中
                if i.split()[0] == user_name:
                    # 查看写入lock文件时的时间,如果超过三分钟就把他删除,如果没有超过三分钟,就拒绝登陆
                    date_old = "%s %s" % (i.split()[1], i.split()[2])
                    if date_compare(date_old, login_time):
                        if_passwd_ok(user_name, user_passwd, count)
                        # 删除用户。用linux下的sed
                        os.system("sed -i '/%s/d' user_lock" % user_name)

                        status1 = 1
                        break
                    # 删除用户
                    else:
                        print  "您在三分钟之内登录三次错误,已被锁定,请稍后再试。"
                        status1 = 1
                        break
                else:
                    continue
            if status1 == 0:
                if_passwd_ok(user_name, user_passwd, count)


# datetime.datetime.fromtimestamp(time.mktime(time.strptime(stringDate,"%Y-%m-%d %H:%M:%S")))





print "欢迎登陆系统".center(100)
user_name = raw_input("请输入用户名:")
#user_passwd=raw_input("enter your passwd")
user_passwd = getpass.getpass("请输入密码:")
d1 = datetime.datetime.now()
login_time = str(d1).split(".")[0]
if_lock(user_name, user_passwd, login_time)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值