xml,configpraser,hashlib模塊

寫一個XML文件

import xml.etree.ElementTree as ET
namelist=ET.ElementTree("namelist")

namelist生成一個根節點
name=ET.SubElement(namelist,"name",attrib={"strinf":"yes","name":"alex"})

賦予namelist屬性
age=ET.SubElement(name,"age")

在根下面建立子節點
age.txt=27

賦值
role=ET.SubElement(name,"role")

根下建立子節點
role.txt="teacher"

et=ET.ElementTree(namelist)

生成文件對象
et.write("test.xml",encoding="utf-8",xml_declaration=True)

寫進一個xml文件

 

 

configparser模塊:可以生成和修改配置文件

import configparser
config=configparser.ConfigParser()#生成對象
config["DEFAULT"]={'Interval':'45',
                 'Compression':'yes',
                 'CompressionLevel':'9'}

賦予屬性,生成字典
config['bitbucket.org']={}可以單獨生成
config['bitbucket.org']['User']='alex'#賦值
config['topsecret.server.com']={}
topsecret=config['topsecret.server.com']
topsecret['Host Port']='50022'
topsecret['ForwardXll']='no'
topsecret['enabled']='YES'

config['DEFAULT']['ForwardXll']='yes'
f=open("config.ini",'w')
config.write(f)
f.close()

 

在生成的文件中增刪改

import configparser
config=configparser.ConfigParser()
config.read("config1.ini")
print(config.sections())#找到文件的sections
config_name=config.sections()[1]

#找到對應的值
print(config[config_name]["host port"])

sec=config.remove_option(config_name,'forwardxll')
刪除forwardx11這一行

config.set(config_name,'host port','3000')

將端口號改爲3000
config.write(open("config2.ini","w"))

 

hashlib模塊:用來驗證文件內容一致性的

import hashlib
m= hashlib.md5()
m.update(b"alex")
print(m.hexdigest())
m.update(b"li")
print(m.hexdigest())

讀一行的md5沒有比讀整篇的md5省內存

m2=hashlib.md5()
m2.update(b"alexli")
print(m2.hexdigest())

 

m2=hashlib.sha256()
m2.update(b"alexli")
print(m2.hexdigest())

sha256的比md5的安全

加鹽算法,更安全

import hmac
hamc_name=hmac.new(b"salt",b"hello")
print(hamc_name.hexdigest())

 

 

 

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章