作业17

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

import hashlib
m=hashlib.md5()
def register():
    username=input('请输入账号:').strip()
    password=input('请输入密码:').strip()

    m.update(password.encode('utf-8'))
    res=m.hexdigest()

    with open('a.txt', 'a', encoding='utf-8')as f:
        f.write(f'{username}:{res}\n')
register()

user_list={}
def login():
    while True:
        username=input('请输入账号:').strip()
        password = input('请输入密码:').strip()
        with open('a.txt','r',encoding='utf-8')as f:
            for line in f:
                user,pwd=line.strip().split(':')
                user_list[user]=pwd
        print(user_list)
        if username not in user_list:
            print('账户不存在')
            continue
        m.update(password.encode('utf-8'))
        res = m.hexdigest()
        if res==user_list.get(user):
            print('登录成功')
            break
        else:
            print('登陆失败')

login()

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

import hashlib
m = hashlib.md5()
def a():
    with open("a.txt","rt",encoding="utf-8")as f :
        l = [10, 20, 30]
        for i in l :
            f.seek(i,0)
            res = f.read(100)
            m.update(res.encode("utf-8"))
        res = m.hexdigest()
        return res

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

注册功能改用json实现

import json
def register():
    username=input('请输入账号:').strip()
    password=input('请输入密码:').strip()

    with open('a.txt', 'a', encoding='utf-8')as f:
        json.dump([username,password],f)
        f.write('\n')
    print('注册成功')


register()

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

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

[section2]
k1 = v1
#a.py
import configparser
config=configparser.ConfigParser()
config.read('text.ini')
print(config.options('section1'))
print(config.items('section1'))
print(config.get('section1','user'))
print(config.getint('section1','age'))
print(config.getfloat('section1','salary'))
posted @ 2020-03-31 22:37  小子,你摊上事了  阅读(64)  评论(0)    收藏  举报