ConfigParser模块
configparser模块主要是用来生成和修改配置文件
比如要生成一个example1.ini的配置文件可以如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import configparser 2 3 config = configparser.ConfigParser() 4 config["DEFAULT"]= {'ServerAliveInterval':'45', 5 'Compression':'yes', 6 'CoompressiinLevel':'9'} 7 config['bitbucket.org'] = {} 8 config['bitbucket.org']['User'] = 'hg' 9 10 config['topsevret.server.com'] = {} 11 topsectret = config['topsevret.server.com'] #这里只不过是更换了一个变量来赋值 12 topsectret['Host Port'] = '4001' 13 topsectret['Forwardx11'] = "no" 14 config['DEFAULT']['ForwardX11'] = 'yes' 15 16 config["mypython.com"] = {'name':'peng', 17 'age':'25', 18 'job':'IT'} 19 config["mypython.com"]['job'] = 'AI' 20 21 with open("example1.ini",'w') as configfile: 22 config.write(configfile)
但大部分时间配置文件都是用手动修改,然后我们的工程代码里面是读出来的,例如:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import configparser 2 3 conf = configparser.ConfigParser() 4 conf.read("example1.ini") 5 6 print(conf.defaults()) 7 print(conf.sections()) 8 print(conf['mypython.com']['age'])
那么读出来的结果如下:
Hashlib模块
Hashlib模块包括MD5和SHA, 包括了SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法,一般来说SHA比MD5算法更好,且SHA512比SHA1、SHA224等更优秀;
另外,MD5算法是反解不了,有些可以得出明文,实际是撞库等方式得到的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #md5其实是不能反解的,有的能输出明文是用撞库等方式得到的 2 import hashlib 3 4 m = hashlib.md5() 5 m2 = hashlib.md5() 6 m.update(b"peng") #1b77697b886efcf3fe95a8839064c1cb 7 print(m.hexdigest()) 8 m.update(b"junfei") #其实这里生成的MD5是pengjunfei的加在一起的计算结果 f2e1ce014a4fdab21b2fb78d782e3de5 9 print(m.hexdigest()) 10 11 m2.update(b"pengjunfei") #f2e1ce014a4fdab21b2fb78d782e3de5 12 print(m2.hexdigest())
hmac模块
消息加密中使用,但实际使用过程,得发送和接收的双方知道key。这个加密模块的加密方式比较快。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import hmac 2 3 h_test = hmac.new("PengDeTesila".encode(encoding="utf-8")) 4 print(h_test.digest()) 5 print(h_test.hexdigest())