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())

 


三.

posted @ 2017-08-21 14:08  一晃而过的少年  阅读(122)  评论(0编辑  收藏  举报