day7 補充模塊configparser/hashlib
阿新 • • 發佈:2017-08-21
passwd tint span 用法 date utf-8 print 哈希 .get
一.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())
三.
day7 補充模塊configparser/hashlib