一、configparser模块:
*创建
import configparser
config = configparser.ConfigParser() #创建对象。这个对象类似空字典
#创建第一个键值对
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
#创建第二个键值对
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
#创建第二个键值对
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'
topsecret['ForwardX11'] = 'no'
#下述方式比较特殊
with open('example.ini', 'w') as f: #打开文件example.ini
config.write(f) #把conifg写到文件中
运行结果:example.ini
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
*创建
import configparser
config = configparser.ConfigParser() #创建对象。这个对象类似空字典
#创建第一个键值对
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}
#创建第二个键值对
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
#创建第二个键值对
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'
topsecret['ForwardX11'] = 'no'
#下述方式比较特殊
with open('example.ini', 'w') as f: #打开文件example.ini
config.write(f) #把conifg写到文件中
运行结果:example.ini
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9
[bitbucket.org]
user = hg
user = hg
[topsecret.server.com]
host port = 50022
forwardx11 = no
#查看(注意default的特殊性,同时只要把config按照字典来理解就可以了)
import configparser
config = configparser.ConfigParser()
config.read('example.ini') #读取对应的文件
print(config.sections()) #['bitbucket.org', 'topsecret.server.com'],默认(default)的不会被打印
print('bytebong.com' in config)# False 判断bytebong.com是不是在config中(即通过文件中是否有这个key值-->bytebong.com)
print(config['bitbucket.org']['User']) # hg
print(config['DEFAULT']['Compression']) #yes
print(config['topsecret.server.com']['ForwardX11']) #no
for key in config['bitbucket.org']:
print(key)
print(config.options('bitbucket.org'))
#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] 默认的default对应的字典的key会被打印
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
print(config.get('bitbucket.org','compression'))
#yes 通过bitbucket.org找到value,value也是字典形式,所以通过compression找到对应的value
#添加
import configparser
config = configparser.ConfigParser()
config.add_section('yuan')
config.set('yuan','k1','11111')
config.write(open('i.cfg', "w"))
运行效果:
[yuan]
k1 = 11111
#删除
import configparser
config = configparser.ConfigParser()
config.remove_section('topsecret.server.com') #删除topsecret.server.com模块(key与value都删除)
config.remove_option('bitbucket.org','user')#bitbucket.org-->value(此value是字典形式)-->删除value的key(user)对应的数值
#修改
import configparser
config = configparser.ConfigParser()
config.read("i.cfg")
config.set('yuan','k1','qwer')
config.write(open('i.cfg', "w"))
host port = 50022
forwardx11 = no
#查看(注意default的特殊性,同时只要把config按照字典来理解就可以了)
import configparser
config = configparser.ConfigParser()
config.read('example.ini') #读取对应的文件
print(config.sections()) #['bitbucket.org', 'topsecret.server.com'],默认(default)的不会被打印
print('bytebong.com' in config)# False 判断bytebong.com是不是在config中(即通过文件中是否有这个key值-->bytebong.com)
print(config['bitbucket.org']['User']) # hg
print(config['DEFAULT']['Compression']) #yes
print(config['topsecret.server.com']['ForwardX11']) #no
for key in config['bitbucket.org']:
print(key)
print(config.options('bitbucket.org'))
#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11'] 默认的default对应的字典的key会被打印
print(config.items('bitbucket.org')) #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]
print(config.get('bitbucket.org','compression'))
#yes 通过bitbucket.org找到value,value也是字典形式,所以通过compression找到对应的value
#添加
import configparser
config = configparser.ConfigParser()
config.add_section('yuan')
config.set('yuan','k1','11111')
config.write(open('i.cfg', "w"))
运行效果:
[yuan]
k1 = 11111
#删除
import configparser
config = configparser.ConfigParser()
config.remove_section('topsecret.server.com') #删除topsecret.server.com模块(key与value都删除)
config.remove_option('bitbucket.org','user')#bitbucket.org-->value(此value是字典形式)-->删除value的key(user)对应的数值
#修改
import configparser
config = configparser.ConfigParser()
config.read("i.cfg")
config.set('yuan','k1','qwer')
config.write(open('i.cfg', "w"))
二、Hashlib模块
1.
import hashlib
obj=hashlib.md5() #md5算法
obj.update("admin".encode("utf8"))
print(obj.hexdigest()) ##21232f297a57a5a743894a0e4a801fc3
注释:把admin字符串按照md5算法转换成一定长度的字符串!不管需要转换的字符串多长,都会转换成固定长度的字符串!
2.
import hashlib
obj=hashlib.md5() #md5算法
obj.update("admin".encode("utf8"))
print(obj.hexdigest())
obj.update("root".encode("utf8"))
print(obj.hexdigest())# 4b3626865dc6d5cfe1c60b855e68634a
注释:上述代码相当于把adminroot转换成固定长度的字符串!并不是单纯的把root转换成固定长度的字符串!
总结:hashlib模块就是把不定长的str按照算法转换成固定长度的str!而且只能加密不能解密!
import hashlib
obj=hashlib.md5() #md5算法
obj.update("admin".encode("utf8"))
print(obj.hexdigest()) ##21232f297a57a5a743894a0e4a801fc3
注释:把admin字符串按照md5算法转换成一定长度的字符串!不管需要转换的字符串多长,都会转换成固定长度的字符串!
2.
import hashlib
obj=hashlib.md5() #md5算法
obj.update("admin".encode("utf8"))
print(obj.hexdigest())
obj.update("root".encode("utf8"))
print(obj.hexdigest())# 4b3626865dc6d5cfe1c60b855e68634a
注释:上述代码相当于把adminroot转换成固定长度的字符串!并不是单纯的把root转换成固定长度的字符串!
总结:hashlib模块就是把不定长的str按照算法转换成固定长度的str!而且只能加密不能解密!
本文详细介绍了Python中ConfigParser模块的使用方法,包括配置文件的创建、读取、修改、添加及删除操作;同时深入探讨了Hashlib模块的工作原理,演示了如何利用md5算法进行字符串加密。
3768

被折叠的 条评论
为什么被折叠?



