zoukankan      html  css  js  c++  java
  • day7 补充模块configparser/hashlib

    一.configparser 模块介绍
    1.1此模块主要解析以下配置文件

     [egon]
      name=egon
      age=18
      is_admin=True
      salary=3.1


    1.2 以下是此模块的用法

    import configparser
    
    config=configparser.ConfigParser()
    config.read('a.ini')
    
    #取配置
    # print(config.sections()) #看标题
    # print(config.options(config.sections()[0])) #查看某个标题下的配置项
    # res=config.get('alex','name')#查看某个标题下的某个配置项的值
    # print(type(res))
    
    # res1=config.getint('egon','age')#查看某个标题下的某个配置项的值
    # print(type(res1))
    #
    # res1=config.getboolean('egon','is_admin')#查看某个标题下的某个配置项的值
    # print(type(res1))
    #
    # res1=config.getfloat('egon','salary')#查看某个标题下的某个配置项的值
    # print(type(res1))
    
    
    #修改
    # config.remove_section('alex')
    # config.remove_option('egon','age')
    
    config.add_section('alex')
    config.set('alex','name','SB')
    
    
    config.write(open('a.ini','w'))



    二、hashlib  哈希模块
    一种算法 ,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
    三个特点:
    1.内容相同则hash运算结果相同,内容稍微改变则hash值则变
    2.不可逆推
    3.相同算法:无论校验多长的数据,得到的哈希值长度固定。

    import hashlib
    #
    # m=hashlib.md5()
    # m.update('hello'.encode('utf-8'))
    # m.update('world'.encode('utf-8'))
    # print(m.hexdigest())
    #
    #
    # m=hashlib.md5()
    # m.update('helloworld'.encode('utf-8'))
    # print(m.hexdigest())
    #
    # m=hashlib.md5('helloworld'.encode('utf-8'))
    # print(m.hexdigest())
    #
    #
    # m=hashlib.md5('h'.encode('utf-8'))
    # m.update('elloworld'.encode('utf-8'))
    # print(m.hexdigest())
    # m=hashlib.md5()
    # with open('a.xml','rb') as f:
    #     for line in f:
    #         m.update(line)
    # print(m.hexdigest())
    #
    #
    # #耗费内存不推荐使用
    # m=hashlib.md5()
    # with open('a.xml','rb') as f:
    #     m.update(f.read())
    # print(m.hexdigest())
    
    
    
    
    
    #加盐
    # password='alex3714'
    # m=hashlib.md5('yihangbailushangqingtian'.encode('utf-8'))
    # m.update(password.encode('utf-8'))
    #
    # passwd_md5=m.hexdigest()
    #
    # print(passwd_md5)
    
    
    
    import hmac
    
    h=hmac.new('hello'.encode('utf-8'))
    h.update('world'.encode('utf-8'))
    print(h.hexdigest())
    
    h=hmac.new('hello'.encode('utf-8'))
    h.update('w'.encode('utf-8'))
    h.update('or'.encode('utf-8'))
    h.update('ld'.encode('utf-8'))
    print(h.hexdigest())


    三.

  • 相关阅读:
    nmap扫描工具
    cobbler全自动批量安装部署linux
    使用ngxtop实时监控nginx
    Nginx 错误汇总
    定制sudo的密码保持时间以及如何不需要密码
    解决eclipse中出现Resource is out of sync with the file system问题
    log4j:ERROR Category option " 1 " not a decimal integer.错误解决
    properties文件中中文不能显示或者中文乱码
    Log4j使用总结
    eclipse, Log4j配置(真心的详细~)
  • 原文地址:https://www.cnblogs.com/wangshaojie/p/7404096.html
Copyright © 2011-2022 走看看