• 常见模块作业


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

    import hashlib
    m = hashlib.md5()
    def register():
        inp_name = input("请输入账号:").strip()
        inp_pwd = input("请输入密码:").strip()
        re_inp_pw = input("请再次输入密码:").strip()
        if inp_pwd==re_inp_pw:
            m.update(inp_pwd.encode("utf-8"))
            res = m.hexdigest()
            # print(res)
            with open("a.txt","a",encoding="utf-8")as f:
                f.write(f'{inp_name}:{res}
    ')
    
    def login():
        inp_name = input('输入账号').strip()
        inp_pwd = input('输入密码').strip()
        res = m.update(inp_pwd.encode('utf-8'))
        with open('a.txt','rt',encoding='utf-8')as f:
            for i in f:
                name,pwd = i.strip().split(':')
                if res == pwd and inp_name == name:
                    print('登入成功')
                    break
            else:
                print('登入失败')

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

    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("文件不完整")

    3、注册功能改用json实现

    def register():
        inp_name = input("请输入账号:").strip()
        inp_pwd = input("请输入密码:").strip()
        re_inp_pw = input("请再次输入密码:").strip()
        if inp_pwd==re_inp_pw:
            with open("a.txt","a",encoding="utf-8")as f:
                json.dump(f'{inp_name}:{inp_pwd}',f)

    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'))
  • 相关阅读:
    算术运算
    数据分析
    科学计算
    面向对象
    文件操作-py
    pillow图像处理
    模块
    固定翼飞行过程产生的阻力
    修改行间距等基本操作
    文件操作
  • 原文地址:https://www.cnblogs.com/bk134/p/12607772.html
Copyright © 2020-2023  润新知