python第十九天——感冒中

ConfigParser模块,hashlib模块,hmac模块

创建配置文件:

 1 import configparser
 2 
 3 config = configparser.ConfigParser()#创建一个配置文件的对象变量
 4 #全局配置
 5 config["DEFAULT"] = {'ServerAliveInterval': '45',
 6                       'Compression': 'yes',
 7                      'CompressionLevel': '9'}
 8 #新建一个域名
 9 config['uge3.cn'] = {}
10 uge3=config['uge3.cn']
11 uge3['User'] = 'yjj'
12 
13 config['topsecret.server.com'] = {}
14 topsecret = config['topsecret.server.com']
15 topsecret['Host Port'] = '50022'     # mutates the parser
16 topsecret['ForwardX11'] = 'no'  # same here
17 
18 config['DEFAULT']['ForwardX11'] = 'yes'
19 with open('example.ini', 'w') as configfile:
20    config.write(configfile)#配置文件写入打开的文档

查看:

import configparser
config = configparser.ConfigParser()#创建一个配置文件的对象变量

config.read('example.ini')#读取文件
print(config.sections())#输出相关内容
node_name=config.sections()[1]
print(config[node_name])
for i,v in config[node_name].items():#可以循环输出
    print(i,v)


print(config.options('uge3.cn'))#打印所选域名信息与全息信息

print(config.items('topsecret.server.com'))#打印所选域名信息值与全息信息、值

修改,添加,删除:

 1 import configparser
 2 config = configparser.ConfigParser()#创建一个配置文件的对象变量
 3 
 4 config.read('example.ini')#读取文件
 5 node_name=config.sections()[1]
 6 print(config[node_name])
 7 config.remove_option(node_name,'forwardx11')#删除指定条目
 8 config.set(node_name,'host port','445555')
 9 config.write(open('example_2.ini','w'))#重写文件
10 sec = config.has_section('wupeiqi')#查找内容
11 print(sec)
12 sec = config.add_section('wupeiqi')#添加内容
13 config.has_section('wupeiqi2')#查找内容
14 config.add_section('wupeiqi2')#添加内容
15 config.write(open('i.cfg', "w"))#重写文件

 hashlib模块:

加密类型:MD5,SHA1,SHA224,SHA256,SHA384,SHA512

 1 import hashlib
 2 m=hashlib.md5()#使用MD5方法
 3 m.update(b'yan')#对字符串进行MD5值的对应算法
 4 print(m.hexdigest())#用十六进制输出
 5 m.update(b'jingjing')
 6 print(m.hexdigest())#41e76e38a109317422894a86ed970288
 7 m2=hashlib.md5()#使用MD5方法
 8 m2.update(b'yanjingjing')#对字符串进行MD5值的对应算法
 9 print(m.hexdigest())#41e76e38a109317422894a86ed970288
10 #相同的字符串,md5永远一样

hmac模块:

1 h=hmac.new(b'123',b'BCD')#它内部对我们创建 key 和 内容 再进行处理然后再加密
2 print(h.hexdigest())

您的资助是我最大的动力!
金额随意,欢迎来赏!

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的关注我

如果,想给予我更多的鼓励,求打

因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【莫柔落切】!

联系或打赏博主【莫柔落切】!https://home.cnblogs.com/u/uge3/

原文地址:https://www.cnblogs.com/uge3/p/6886407.html