23-----我是作业

# 1、把登录与注册的密码都换成密文形式
import hashlib
def register():
    user = input('name:')
    password = input('password:')
    pwd = hashlib.md5(password.encode('utf-8'))
    pwd1 = pwd.hexdigest()
    with open('info','ab') as f:
        f.write(f'{user}:{pwd1}\n'.encode('utf-8'))
register()

def login():
    ipt_account = input('Enter account:')
    ipt_pwd = input('Enter password:')
    pwd_hash = hashlib.md5(ipt_pwd.encode('utf-8'))
    with open('db.txt','rt',encoding='utf-8') as f:
        for i in f:
            if [ipt_account,pwd_hash.hexdigest()] == i.strip('\n').split(':'):
                print('successful')
                break
        else:
            print('error')
# 2、文件完整性校验(考虑大文件)
import hashlib
status = []
def test(start=30,times=0):
    if times < 5:
        with open('a','rb') as f,open('b','rb') as w:
            f.seek(start)
            w.seek(start)
            f_m = hashlib.md5(f.read(300))
            w_m = hashlib.md5(w.read(300))
            f_res = f_m.hexdigest()
            w_res = w_m.hexdigest()
            if f_res == w_res:
                status.append('t')
            else:
                status.append('f')
        times += 1
        start += 300
        test(start,times)

test()
if 'f' in status:
    print('失败')
else:
    print('成功')
# 3、注册功能改用json实现
import json
def log():
    user = input('name').strip()
    pwd = input('password').strip()
    with open('a','a',encoding='utf-8') as f:
        json.dump(f'{user}:{pwd}',f)
        f.write('\n')
log()
# 4、项目的配置文件采用configparser进行解析
import configparser
config = configparser.ConfigParser()
config.read('test.ini')

# 获取所有section
print(config.sections())
# 获取section所有option
print(config.options('section1'))
# 获取每个option与其对应的值
print(config.items(section='section1'))
# 获取option对应的值
print(config.get('section1','k1'))
print(config.getfloat('section1','k3'))
print(config.getboolean('section1','k2'))
posted @ 2020-03-31 20:29  微信搜索-程序媛小庄  阅读(119)  评论(0)    收藏  举报