3-31作业

作业:

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

import hashlib
def register():
    m = hashlib.md5()
    username = input("请输入要注册的账号:")
    password = input("请输入要注册的密码:")
    password2 = input("请再次输入的密码:")
    if password==password2:
        m.update(password.encode("utf-8"))
        res = m.hexdigest()
        print(res)
        with open("db.txt","a",encoding="utf-8")as f:
            f.write(f'{username}:{res}\n')

def login():
    user_inp = input("请输入你的账号:")
    pwd_inp = input("请输入你的密码:")
    with open("db.txt","r",encoding="utf-8")as f1:
        user = hashlib.md5()
        user.update(pwd_inp.encode("utf-8"))
        res = user.hexdigest()
        for line in f1:
            username,password = line.strip().split(":")
            if user_inp ==username:
                if res == password:
                    print("登录成功")
                    return
                else:
                    print('密码错误')
        else:
            print("账号不存在")
register()
login()

 


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

import hashlib

def down_file():
    with open('download_file.txt', 'rb') as f:
        m = hashlib.md5()
        for i in [0, 150, 300]:
            f.seek(i, 0)
            res = f.read(50)
            m.update(res)
        res = m.hexdigest()
    return res

def o_file():
    with open('file.txt', 'rb') as f:
        m = hashlib.md5()
        for i in [0, 150, 300]:
            f.seek(i, 0)
            res = f.read(50)
            m.update(res)
        res = m.hexdigest()
        return res

if down_file() == o_file():
    print("文件完整")
else:
    print("文件已损坏")

 


3、注册功能改用json实现

import json
import os
def register():
    inp_name = input("用户名:").strip()
    inp_pwd = input("密码:").strip()
    inp_pwd2 = input("确认密码:").strip()
    if not (inp_name and inp_pwd and inp_pwd2):
        print('输入不能为空')
        return
    if inp_pwd != inp_pwd2:
        print('两次密码不一致')
        return
    with open(r'db.json', 'a+t', encoding='utf-8') as r_f,\
        open(r'db.json.swap', 'wt', encoding='utf-8') as w_f:
        r_f.seek(0, 0)
        if r_f.read() == '':
            r_f.write(json.dumps({inp_name:inp_pwd}))
            w_f.close()
            os.remove('db.json.swap')
            print("注册成功")
            return
        r_f.seek(0, 0)
        load_res = json.loads(r_f.read())
        if inp_name in load_res:
            print('用户名已存在')
            w_f.close()
            os.remove('db.json.swap')
            return
        load_res[inp_name] = inp_pwd
        json.dump(load_res, w_f)
        print("注册成功")
    os.remove('db.json')
    os.rename('db.json.swap', 'db.json')
register()

 


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

 
import configparser

config=configparser.ConfigParser()
config.read(settings.py)
print(config.sections())

print(config.options('section1'))

print(config.items('section1'))

 

posted @ 2020-03-31 19:16  Only-W  阅读(105)  评论(0编辑  收藏  举报