• python之路--day18--hashlib,subprocess,configparser模块


    hashlib模块

    1,什么叫hash

      hash是一种算法,接收传入的内容,经过运算得到一串hash值

    2,hash值得特点

      1.只要传入的内容一样,得到的hash值必然一样------文件完整性校验

    # import hashlib
    #
    # m=hashlib.md5()
    # m.update('hello'.encode('utf-8'))
    # m.update('world'.encode('utf-8'))
    # m.update('egon'.encode('utf-8'))
    # print(m.hexdigest()) #3801fab9b8c8d9fcb481017969843ed5
    特点1

      2.不能由hash值返解出内容

      3.只要使用的hash算法不变,无论校验的内容有多大,得到的hash值长度是固定的

    3,密码加盐

      使用update添加其他字符在密码里,使破解密码的难度加大

    import hmac
    m=hmac.new('天王盖地虎,小鸡炖模块'.encode('utf-8'))
    m.update('alex3814'.encode('utf-8'))
    print(m.hexdigest())

    subprocess模块

       subprocess模块提供了一种一致的方法来创建和处理附加进程,可以调用系统命令。

     subprocess.Popen()方法,可以获得系统命令执行后的结果,通过管道PIPE接收。

      stdout=subprocess.PIPE   接收系统命令执行成功后的结果

      stderr=subprocess.PIPE 接收系统命令执行失败后的结果

    import subprocess
    
    obj=subprocess.Popen('dir',
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE
                         )
    subprocess接收系统命令

    configparser模块

    import configparser
    
    config=configparser.ConfigParser()
    config.read('a.cfg')
    
    #查看所有的标题
    res=config.sections() #['section1', 'section2']
    print(res)
    
    #查看标题section1下所有key=value的key
    options=config.options('section1')
    print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
    
    #查看标题section1下所有key=value的(key,value)格式
    item_list=config.items('section1')
    print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]
    
    #查看标题section1下user的值=>字符串格式
    val=config.get('section1','user')
    print(val) #egon
    
    #查看标题section1下age的值=>整数格式
    val1=config.getint('section1','age')
    print(val1) #18
    
    #查看标题section1下is_admin的值=>布尔值格式
    val2=config.getboolean('section1','is_admin')
    print(val2) #True
    
    #查看标题section1下salary的值=>浮点型格式
    val3=config.getfloat('section1','salary')
    print(val3) #31.0
    读取
    import configparser
    
    config=configparser.ConfigParser()
    config.read('a.cfg',encoding='utf-8')
    
    
    #删除整个标题section2
    config.remove_section('section2')
    
    #删除标题section1下的某个k1和k2
    config.remove_option('section1','k1')
    config.remove_option('section1','k2')
    
    #判断是否存在某个标题
    print(config.has_section('section1'))
    
    #判断标题section1下是否有user
    print(config.has_option('section1',''))
    
    
    #添加一个标题
    config.add_section('egon')
    
    #在标题egon下添加name=egon,age=18的配置
    config.set('egon','name','egon')
    config.set('egon','age',18) #报错,必须是字符串
    
    
    #最后将修改的内容写入文件,完成最终的修改
    config.write(open('a.cfg','w'))
    改写
  • 相关阅读:
    js总结 |数组重复问题
    前端Js自定义相机取景框
    nodejs+mongodb运用
    使用MongoDB
    总结 |异步/非阻塞的处理方式
    npm与依赖包
    js总结 |JS深度拷贝的方法
    js笔记 |整洁代码技巧
    微信支付——介入指引
    2021最全测试资源合集(已更新至2021.03.09,关注测试生财公众号,享受独家爆料)
  • 原文地址:https://www.cnblogs.com/guodengjian/p/8781900.html
Copyright © 2020-2023  润新知