3.31作业

作业:

1、把登录与注册的密码都换成密文形式

login_user = {'user': None}


def login():
    while True:
        import hashlib
        name = input('请输入账号: ').strip()
        res = select(name)
        if not res:
            print(f'账号{name}不存在,请重新输入')
            continue
        elif name == login_user.get('user'):
            print(f'账号{name}已登录')
            continue
        else:
            pwd = input('请输入密码: ').strip()
            m = hashlib.md5()
            pwd = str(pwd)
            m.update(pwd.encode('utf-8'))
            res1 = m.hexdigest()
            if res1 == res[1]:
                print('登录成功!')
                login_user['user'] = name
                break
            else:
                print('密码错误!')
                break


def register():
    while True:
        name = input('请输入账号:').strip()
        res = select(name)
        if not res:
            print('欢迎你的加入!')
            save()
            break
        else:
            print(f'当前账号{name}已注册,请重新输入')


def select(name):
    with open('a.txt', 'r', encoding='utf-8') as f:
        for line in f:
            if name in line:
                data = line.strip().split(':')
                return data


def save():
    import hashlib
    name = input('输入账号: ').strip()
    pwd = input('输入密码: ').strip()
    pwd1 = input('确认密码: ').strip()
    if pwd1 == pwd:
        with open('a.txt', 'at', encoding='utf-8') as f:
            m = hashlib.md5()
            pwd1 = str(pwd1)
            m.update(pwd1.encode('utf-8'))
            res = m.hexdigest()
            f.write(f'{name}:{res}\n')


dic = {
    '1': login,
    '2': register

}


def run():
    print('0 :退出\n'
          '1 :登录\n'
          '2 :注册')
    tage = True
    while tage:
        choice = input('输入数字命令:').strip()
        if choice == '0':
            break
        else:
            dic.get(choice)()


run()

2、文件完整性校验(考虑大文件)

def make_num():
    import random
    res = random.randint(1, 10)
    return res


def get_data(path, num):
    import hashlib
    m = hashlib.md5()
    with open(f'{path}', 'rb') as f:
        f.seek(num, 0)
        data = f.read(10).decode('utf-8')
        m.update(data.encode('utf-8'))
        res1 = m.hexdigest()
        f.seek(num, 1)
        data = f.read(10).decode('utf-8')
        m.update(data.encode('utf-8'))
        res2 = m.hexdigest()
        f.seek(num, 2)
        data = f.read(10).decode('utf-8')
        m.update(data.encode('utf-8'))
        res3 = m.hexdigest()
        return res1, res2, res3


def check():
    num = make_num()
    src_file = input('输入原文件路径: ').strip()
    res1 = get_data(src_file, num)
    print(res1)
    check_file = input('输入待检测文件路径: ').strip()
    res2 = get_data(check_file, num)
    print(res2)
    if res1[0] == res2[0] and res1[1] == res2[1] and res1[2] == res2[2]:
        print('两文件相同')
    else:
        print('检测文件不完整')


check()

3、注册功能改用json实现

def register():
    import json
    dic = {}
    name = input('输入账号: ').strip()
    pwd = input('输入密码: ').strip()
    pwd1 = input('确认密码: ').strip()
    if pwd1 == pwd:
        dic[name] = pwd
        with open('test.json', mode='at', encoding='utf-8')as f:
            json.dump(dic, f)


register()

4、项目的配置文件采用configparser进行解析

import configparser

config = configparser.ConfigParser()
config['interface'] = {}
config['interface']['length'] = '200'
config['interface']['width'] = '100'
config['file_path'] = {}
config['file_path']['src_file'] = 'a.txt'
config['file_path']['dst_file'] = 'b.txt'
with open('setting.ini', 'w') as configfile:

config=configparser.ConfigParser()
config.read('setting.ini')
#查看所有的标题
res=config.sections()
print(res)  #['interface', 'file_path']

#查看标题interface下所有key=value的key
options=config.options('interface')
print(options)  #['length', 'width']

#查看标题interface下user的值=>字符串格式
val=config.get('interface','length')
print(val)  #200

#查看标题interface下length的值=>整数格式
val1 = config.getint('interface', 'length')
print(val1, type(val1))  #200 <class 'int'>

#config.getboolean()   config.getfloat()





# 删除整个标题interface
config.remove_section('interface')

#判断标题file_path下是否有dst_file
print(config.has_option('file_path','dst_file'))

#添加一个标题
config.add_section('typeface')

#在标题typeface下添加name='18'的配置
config.set('typeface','size','18')

#最后将修改的内容写入文件,完成最终的修改
config.write(open('setting.ini','w'))
'''
[interface]
length = 200
width = 100

[file_path]
src_file = a.txt
dst_file = b.txt

[typeface]
size = 18
'''
posted @ 2020-03-31 22:16  风起千寻  阅读(125)  评论(0编辑  收藏  举报