当你发现自己的才华撑不起野心时,就请安静下来学习吧。

Personal site

↑点击传送

3.31作业

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

import hashlib
def register():
    while True:
        user=input('your name:').strip()
        pwd=input('your pwd:').strip()
        pwd1=input('again one:').strip()
        if pwd != pwd1:
            print('两次输入不一致,请重新输入!')
            continue
        m=hashlib.sha3_256()
        m.update(f'{pwd}'.encode('utf-8'))
        pwd=m.hexdigest()
        with open('db.txt','a',encoding='utf-8') as f:
            f.write(f'{user}:{pwd}\n')
            print('注册成功')
            break
# register()
def login():
    user=input('your name:').strip()
    pwd=input('your pwd:').strip()
    m=hashlib.sha3_256()
    m.update(f'{pwd}'.encode('utf-8'))
    pwd=m.hexdigest()
    with open('db.txt','r',encoding='utf-8') as f:
        for line in f:
            name,password=line.strip().split(':')
            if user == name and pwd == password:
                print('login successfully')
                break
        else:
            print('user or pwd error')

login()

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

import hashlib
def copy():
    with open('db.txt','rb') as f1,\
        open('db1.txt','wb') as f2:
        f2.write(f1.read())

copy()

with open('db.txt','rb') as f:
    d = []
    a = f.read(10)
    while a:
        m = hashlib.md5(a)
        d.append(m.hexdigest())
        f.seek(10, 1)
        a = f.read(10)

print(d)
with open('db1.txt','rb') as f:
    d1 = []
    a = f.read(10)
    while a:
        m = hashlib.md5(a)
        d1.append(m.hexdigest())
        f.seek(10,1)
        a = f.read(10)
print(d1)
for i in range(len(d)):
    if d[i] != d1[i]:
        print('文件不完整')
        break
else:
    print('文件完整')
#     3、注册功能改用json实现
import json,hashlib
def register():
    user = input('请输入账号:').strip()
    pwd = input('请输入密码:').strip()
    pwd1 = input('请确认密码:').strip()
    if pwd == pwd1:
        import hashlib
        m = hashlib.md5(pwd.encode('utf-8'))
        m = m.hexdigest()
        with open('db.txt','a',encoding='utf-8') as f:
            json.dump('{}:{}:{}'.format(user,m,0),f)
            print('注册成功')
    else:
        print('密码不一致')
register()

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

import configparser as conf

settings = conf.ConfigParser()
settings.read('conf.txt')
print(settings.sections())
print(settings.items('section1'))
posted @ 2020-04-01 00:15  Joab-0429  阅读(160)  评论(0编辑  收藏  举报