python第22天作业

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

import hashlib


def password(inp_pwd):
    pwd = hashlib.md5()
    pwd.update('你还想破解密码'.encode('utf-8'))
    pwd.update(inp_pwd.encode('utf-8'))
    pwd.update('憨批'.encode('utf-8'))
    user_pwd = pwd.hexdigest()
    return user_pwd


def register():
    inp_name = input("注册账号:>>").strip()
    inp_pwd = input("注册密码:>>").strip()
    inp_pwd2 = input("确认密码:>>").strip()
    if inp_pwd != inp_pwd2:
        print('两次密码不一致')
        return
    with open(r'db.txt', 'rb') as f:
        for line in f:
            x, *_ = line.decode('utf-8').strip().split(':')
            if x == inp_name:
                print('账号已经注册,请重新输入')
        inp_pwd = password(inp_pwd)
        user_data = '{}:{}'.format(inp_name, inp_pwd)
    with open(r'db.txt', 'ab') as f:
        f.write(f'{user_data}\n'.encode('utf-8'))
        print('注册成功')


def login():
    inp_name = input("请输入您的账号:>>").strip()
    inp_pwd = input("请输入您的密码:>>").strip()
    inp_pwd = password(inp_pwd)
    with open(r'db.txt', 'rb') as f:
        for line in f:
            user_data = line.decode('utf-8').strip().split(':')
            if inp_pwd in user_data and inp_name == user_data[0]:
                print('登录成功')
                break
        else:
            print('登录失败')


register()
login()

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

import hashlib


def down_file(down_file_path):
    with open(r'{}'.format(down_file_path), 'rb') as f:
        m = hashlib.md5()
        for i in [0, 500, 1000]:
            f.seek(i, 0)
            res = f.read(200)
            m.update(res)
        msg = m.hexdigest()
        return msg


def now_file(file_path):
    with open(r'{}'.format(file_path), 'rb') as f:
        m = hashlib.md5()
        for i in [0, 500, 1000]:
            f.seek(i, 0)
            res = f.read(200)
            m.update(res)
        msg = m.hexdigest()
        return msg

if down_file(r'abc.txt') == now_file(r'abcd.txt'):
    print("文件完整")
else:
    print("文件已损坏")

3、注册功能改用json实现

import json
import os

def register():
    data_dic = {}
    user_name = input('请输入您的注册账号:>>').strip()
    user_pwd1 = input('请输入您的注册密码:>>').strip()
    user_pwd2 = input('请再次输入您的注册密码:>>').strip()
    if user_pwd1 != user_pwd2:
        print('两次密码不一致')
        return None
    with open(r'db.json','rt',encoding='utf-8') as rf:
        if rf.read() != 0:
            rf.seek(0,0)
            res = json.load(rf)
    data_dic.update(res)
    for i in data_dic:
        if i == user_name:
            print('该账号存在,请重新注册')
            return None
    data_dic[user_name] = user_pwd1
    with open(r'db.json.swap','wt',encoding='utf-8') as wf:
        json.dump(data_dic,wf)

    os.remove('db.json')
    os.rename('db.json.swap', 'db.json')
    print('注册成功')
register()

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

看不懂……

posted @ 2020-03-31 20:55  Lance_王  阅读(137)  评论(0编辑  收藏  举报