PYthon-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、文件完整性校验(考虑大文件)

def official_file_hx():
    with open("official_file","rt",encoding="utf-8")as f :
        l = [20, 30, 40]#l根据需求改变
        m = hashlib.md5()
        for i in l :
            f.seek(i,0)
            res = f.read(5)
            m.update(res.encode("utf-8"))
        res = m.hexdigest()
        return res

def download_file_hx():
    with open("download_file","rt",encoding="utf-8")as f :
        l = [20,30,40]
        m = hashlib.md5()
        for i in l:
            f.seek(i,0)
            msg = f.read(5)
            m.update(msg.encode("utf-8"))
        res = m.hexdigest()
        if res == official_file_hx():
            print("文件完整")
        else:
            print("文件不完整")

download_file_hx()

3、注册功能改用json实现

import json

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')
            user_dict = {username:res}
            json.dump(user_dict, f)
register()


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

text.ini
[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31
[section2]
k1 = v1
import configparser

config = configparser.ConfigParser()

config.read('text.ini')

print(config.sections())

print(config.options('section1'))

print(config.items('section1'))

res= config.get('section1','is_admin')

print(res,type(res))

print(config.getint('section1','age'))

print(config.getfloat('section1','age'))

print(config.getboolean('section1','is_admin'))


posted @ 2020-03-31 19:36  小小码农梦还家  阅读(164)  评论(0编辑  收藏  举报