路飞学城Python-Day21(practise)

时间:2021-10-09 08:13:21
 编写程序, 如下有三点要求:
自定义用户信息数据结构, 写入文件, 然后读取出内容, 利用json模块进行数据的序列化和反序列化
e.g
{
"egon":{"password":"123",'status':False,'timeout':0},
"alex":{"password":"456",'status':False,'timeout':0},
}
定义用户类,定义方法db,例如 执行obj.db可以拿到用户数据结构
在该类中实现登录、退出方法, 登录成功将状态(status)修改为True, 退出将状态修改为False(退出要判断是否处于登录状态).\
密码输入错误三次将设置锁定时间(下次登录如果和当前时间比较大于10秒即不允许登录)
路飞学城Python-Day21(practise)路飞学城Python-Day21(practise)
'''
有bug未及时调整
登出函数有问题
'''
import json
import time
def init_info():
    user_if = {
    "egon":{"password":"123",'status':False,'timeout':0},
    "alex":{"password":"456",'status':False,'timeout':0},
    }
    with open('user_info','w',encoding='utf-8') as f:
        json.dump(user_if,f)


def read_info():
    with open('user_info','r+',encoding='utf-8') as f:
        print(json.load(f,encoding = 'utf-8'))

user_name = ''
class User:

    def db(self):
        '''
        db方法:调用时打印当前用户信息和信息存储数据格式
        :return:
        '''
        with open('user_info', 'r+', encoding='utf-8') as f:
            user_info = json.load(f, encoding='utf-8')
            for k, v in user_info.items():
                print('username:%s -' % k, type(k),
                      'password:',[val for val in v.values()][0], type([val for val in v.values()][0]),
                      'status:',[val for val in v.values()][1], type([val for val in v.values()][1]),
                      'timeout:', [val for val in v.values()][2], type([val for val in v.values()][2]),
                      )

    def login(self):
        '''
        登录
        登录成功将状态(status)修改为True
        密码输入错误三次将设置锁定时间(下次登录如果和当前时间比较大于10秒即不允许登录)[个人理解应该是10秒内不能登录,这里的表述有问题]
        :return:
        '''
        count = 0
        while count < 3:
            username = input('请输入用户名>>>').strip()
            password = input('请输入密码>>>').strip()
            with open('user_info', 'r+', encoding='utf-8') as f:
                user_info = json.load(f, encoding='utf-8')
                user_name_list = []
                for k in user_info.keys():
                    user_name_list.append(k)
                if username in user_name_list and password == user_info.get(username)['password']:
                    if user_info.get(username)['timeout'] - time.time()<0:
                        user_info.get(username)['status'] = True
                        print('用户:%s 登陆成功' % username)
                        user_dict = {username:user_info.get(username)}
                        user_name = username
                        with open('user_info','w+',encoding='utf-8') as f:
                            json.dump(user_info, f)
                        return user_dict[username]
                    else:
                        return '用户账号已锁定!'
                elif count == 2:
                    if username in user_name_list:
                        user_info.get(username)['timeout'] = time.time()+10
                        with open('user_info','w+',encoding='utf-8') as f:
                            json.dump(user_info, f)
                        return  '%s账户已锁定'%username

                    else:
                        return '用户名不存在!'

                else:
                    print('账号或密码错误,还有%s次尝试机会'% (3-(count+1)))
                    count += 1

    def logout(self):
        '''
        退出将状态修改为False(退出要判断是否处于登录状态)
        :return:
        '''
        with open('user_info', 'r+', encoding='utf-8') as f:
            user_info = json.load(f, encoding='utf-8')
        user_list = []
        for k in user_info.keys():
            user_list.append(k)
        if user_name in user_list:
            print('当前登录账户%s'%user_name)
            choice = input('请确认是否退出,是:Y  否:N').strip()
            if choice == 'y'.upper() or choice == 'y'.lower():
                user_info.get(user_name)['status'] = False
                with open('user_info', 'w+', encoding='utf-8') as f:
                    json.dump(user_info, f)
            else:
                pass
        else:
            pass


us = User()
# us.db()
u = us.login()
View Code