内置的configparser模块和hashlib模块

#configparser模块:配置解析模块
import configparser
config = configparser.ConfigParser()       #创建一个配置解析对象
config["DEFAULT"] = {
    "name":"sjy",
    "age":"25",
}         #添加配置内容,类似创建字典,再添加到文件中去
with open("confile.ini","w") as f:       #文件名的扩展名任意
    config.write(f)           #注意配置文件写入和单纯文件写入的不同,单纯文件写入是f.write()

#
config.read("example.ini")    #需要先导入需要操作的配置文件
print(config.sections())      #打印出配置文件中(除了默认的DEFAULT)其他的块名
print("bytebong.com" in config)   #判断这个块名是否在这个配置文件中
print(config["bitbucket.org"]["user"])   #取到块中的具体内容,不区分大小写
for i in config["topsecret.server.com"]:
    print(i)              #不管遍历哪个块,默认的DEFAULT块都会被遍历出来
print(config.options("topsecret.server.com"))   #遍历这个块,并将得到的内容放到列表中,注意默认块也会被遍历
print(config.items("topsecret.server.com"))     #遍历这个块,并以键值对的方式放到字典中
print(config.get("topsecret.server.com","compressionlevel"))  #获取块里的内容

#删,改,增
config.add_section("yuan")       #添加块
config.set("yuan","k1","22")     #添加块中的键值对
config.remove_section("bitbucket.org")    #删除配置文件中的块
config.remove_option("topsecret.server.com","forwardx11")   #删除块中的键值对
config.write(open("new_example.ini","w"))

 

#hashlib模块:用于加密操作,做hash算法,摘要算法
import hashlib
obj = hashlib.md5("53sb".encode("utf8"))   #md5加密,括号里的参数自己设置放在需要加密信息前使得密文改变,防止撞库解密
obj.update("hello".encode("utf8"))   #原生md5加密的密文对应关系固定,自己加点东西,使其与原生不一样
print(obj.hexdigest())          #得到一个密文,密文长度是固定的,不可逆的加密
obj.update("admin".encode("utf8"))   #在上面做过加密后,再次调用,是在原先基础上再加密
print(obj.hexdigest())          #这里得到的密文是helloadmin的密文

hash = hashlib.sha256()          #sha256加密,方法与md5类似
hash.update("hello".encode("utf8"))
print(hash.hexdigest())

 

posted @ 2019-05-18 13:29  saber゛  Views(196)  Comments(0Edit  收藏  举报