zoukankan      html  css  js  c++  java
  • python--写一个用户登录验证程序,文件名account.json

    import json
    import os
    import time as t
    import hashlib

    """
    导入模块的方式有哪几种,官方不推荐哪种?
    import 包名
    import module_a #导入
    from module import xx
    from module.xx.xx import xx as rename #导入后重命令
    from module.xx.xx import * #导入一个模块下的所有方法,不建议使用
    """

    """
    如何让你写的模块可以被系统上任何一个py文件导入
    1,放在第三方库中site-package
    from test_import import *
    print(sum1(1,2))
    """

    """
    写一个用户登录验证程序,文件名account.json,内容如下
    {“expire_date”: “2021-01-01”, “id”: 1234, “status”: 0, “9”: 22, “password”: “abc”}

    1,根据用户输入的用户名&密码,找到对应的json文件,把数据加载出来进行验证
    2,用户名为json文件名,密码为 password。
    3,判断是否过期,与expire_date进行对比。
    4,登陆成功后,打印“登陆成功”,三次登陆失败,status值改为1,并且锁定账号。
    """

    def login():
        count = 1
        while True:
            user_filename = input('请输入您的用户名:')
            password = input('请输入您的密码')
            mulu_path = os.path.dirname(__file__)  # 当前所在的目录
            file_path = os.path.join(mulu_path, user_filename)
            if os.path.isfile(file_path):  # 判断输入文件所在路径是否正确
                with open(file_path, 'r') as f:
                    login_dict = json.load(f)  # 将文件内容读取出来
                if login_dict['status'] == 0 and count < 4:  # 判断是否被锁定and登陆次数是否小于3
                    login_password = login_dict['password']  # 登录密码
                    expire_date_tuple = t.strptime(login_dict['expire_date'], '%Y-%m-%d')  # 将过期时间转为元祖
                    expire_date_timestamp = t.mktime(expire_date_tuple)  # 将过期时间元祖转为时间戳
                    now_timestamp = t.time()  # 当前时间戳
                    if login_password == password and now_timestamp < expire_date_timestamp:  # 密码 和 当前时间小于过期时间都成立(登陆成功)
                        print('登陆成功 ')
                        break
                    else:
                        if count == 3:
                            print('您的账户被锁定1')
                        else:
                            print('密码错误,登录失败请重试,您还剩下{}次机会'.format(3 - count))
                else:
                    print('您的账户被锁定2')
                    with open(file_path, 'w', encoding='utf-8')as f:  # 登录次数超过3次时被锁定
                        login_dict['status'] = 1
                        json.dump(login_dict, f)
                    break
            else:
                print('用户名错误,请重新输入')
            count += 1
    
    
    # login()
    
    
    

    """
    写一个用户登录验证程序,文件名account.json,内容如下
    {“expire_date”: “2021-01-01”, “id”: 1234, “status”: 0, “9”: 22, “password”: “abc”}

    1,根据用户输入的用户名&密码,找到对应的json文件,把数据加载出来进行验证
    2,用户名为json文件名,密码为 password。
    3,判断是否过期,与expire_date进行对比。
    4,登陆成功后,打印“登陆成功”,三次登陆失败,status值改为1,并且锁定账号。
    """

    def login_youhua():
        count = 1
        while count < 4:  # 循环 3次
            uesrname = input('请输入你的用户名:')
            password = input('请输入您的密码: ')
            mulu_path = os.path.dirname(__file__)  # 当前目录路径
            file_path = os.path.join(mulu_path, uesrname)  # 拼接成文件的路径
            try:  # 放在try中执行
                fr = open(file_path, 'r')
                dict_file = json.load(fr)
                fr.close()
    
                guoqishijian_tuple = t.strptime(dict_file['expire_date'], '%Y-%m-%d')  # 时间 元祖
                guoqishijian = t.mktime(guoqishijian_tuple)  # 过期时间的时间戳
                now_time = t.time()  # 当前时间的 时间戳
                if dict_file['status'] == 0:  # 判断是否状态为0
                    if password == dict_file['password']:  # 判断是否登陆成功
                        if now_time < guoqishijian:  # 判断是否过期
                            print('登陆成功')
                            break
                        else:
                            print('已过期')
                            break
                    else:
                        if count == 3:  # 如果输入了三次将dict_file['status'] = 1写入文件
                            print('账号已锁定')
                            with open(file_path, 'w', encoding='utf-8')as f:
                                dict_file['status'] = 1
                                json.dump(dict_file, f)
                            break
                        else:  # 否则提示密码错误
                            print('密码错误')
                else:
                    print('已锁定')  # 否则提示已过期退出程序
                    break
    
            except Exception as e:
                print('出错了{}'.format(e.args))
            count += 1  # count放在这里,如果文件名输入错误三次也退出
    
    
    # login_youhua()
    

    """
    把第3题用户密码进行hashlib加密处理。即:json文件里的密码保存为md5的值,然后用md5的值进行验证账号信息是否正确。
    """

    def login_youhua_hash():
        count = 1
        m = hashlib.md5()
        while count < 4:  # 循环3次
            uesrname = input('请输入你的用户名:')
            password_ming = (input('请输入您的密码:'))
            m.update(password_ming.encode('utf-8'))  # 将输入的密码传入到md5的对象m中
            password = m.hexdigest()  # 将16进制形式的密码赋值给password
            mulu_path = os.path.dirname(__file__)  # 当前目录路径
            file_path = os.path.join(mulu_path, uesrname)  # 拼接成文件的路径
            try:  # 放在try中执行
                fr = open(file_path, 'r')
                dict_file = json.load(fr)
                fr.close()
    
                guoqishijian_tuple = t.strptime(dict_file['expire_date'], '%Y-%m-%d')  # 时间 元祖
                guoqishijian = t.mktime(guoqishijian_tuple)  # 过期时间的时间戳
                now_time = t.time()  # 当前时间的 时间戳
                if dict_file['status'] == 0:  # 判断是否状态为0
                    if password == dict_file['password']:  # 判断是否登陆成功
                        if now_time < guoqishijian:  # 判断是否过期
                            print('登陆成功')
                            break
                        else:
                            print('已过期')
                            break
                    else:
                        if count == 3:  # 如果输入了三次将dict_file['status'] = 1写入文件
                            print('账号已锁定')
                            with open(file_path, 'w', encoding='utf-8')as f:
                                dict_file['status'] = 1
                                json.dump(dict_file, f)
                            break
                        else:  # 否则提示密码错误
                            print('密码错误')
                else:
                    print('已锁定')  # 否则提示已过期退出程序
                    break
    
            except Exception as e:
                print('出错了{}'.format(e.args))
            count += 1  # count放在这里,如果文件名输入错误三次也退出
    
    
    login_youhua_hash()
    

  • 相关阅读:
    c# 24种设计模式
    .net如何处理高并发socket,建立高性能健壮的socket服务
    对于devexpress gridview 内插图加加进度条等的一点解读
    devexpress 如何读demo源码 总结
    DevExpress之TreeList节点绑定图片
    DevExpress LookUpEdit 下拉框基本操作
    dev NavBarControl控件
    DevExpress如何实现皮肤的添加及本地化
    vs2015未能计算子级
    c#networkcomms protobuf-net 文件加载出现问题
  • 原文地址:https://www.cnblogs.com/shanshan-test/p/12640084.html
Copyright © 2011-2022 走看看