1.config
configparse
import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9',
'ForwardX11':'yes'
}
config['bitbucket.org'] = {'User':'hg'}
config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
with open('example.ini', 'w') as f:
config.write(f)
import configparser
config = configparser.ConfigParser()
---------------------------查找文件内容,基于字典的形式
print(config.sections()) # []
config.read('example.ini')
print(config.sections()) ['bitbucket.org', 'topsecret.server.com']
print('bytebong.com' in config) False
print('bitbucket.org' in config) True
print(config['bitbucket.org']["user"]) hg
print(config['DEFAULT']['Compression']) yes
print(config['topsecret.server.com']['ForwardX11']) no
print(config['bitbucket.org']) <Section: bitbucket.org>
for key in config['bitbucket.org']: 注意,有default会默认default的键
print(key)
print(config.options('bitbucket.org')) 同for循环,找到'bitbucket.org'下所有键
print(config.items('bitbucket.org')) 找到'bitbucket.org'下所有键值对
print(config.get('bitbucket.org','compression')) # yes get方法Section下的key对应的value
import configparser
config = configparser.ConfigParser()
config.read('example.ini') # 读文件
config.add_section('yuan') # 增加section
config.remove_section('bitbucket.org') 删除一个section
config.remove_option('topsecret.server.com',"forwardx11") 删除一个配置项
config.set('topsecret.server.com','k1','11111')
config.set('yuan','k2','22222')
f = open('new2.ini', "w")
config.write(f) 写进文件
f.close()
2.logging
login 登录
log 日志
logging
什么叫日志?
日志 用来记录用户行为 或者 代码的执行过程
print
logging
我能够“一键”控制
排错的时候需要打印很多细节来帮助我排错
严重的错误记录下来
有一些用户行为 有没有错都要记录下来
import logging
logging.basicConfig(level=logging.WARNING,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
try:
int(input('num >>'))
except ValueError:
logging.error('输入的值不是一个数字')
logging.debug('debug message') # 低级别的 # 排错信息
logging.info('info message') # 正常信息
logging.warning('warning message') # 警告信息
logging.error('error message') # 错误信息
logging.critical('critical message') # 高级别的 # 严重错误信息
print('%(key)s'%{'key':'value'})
print('%s'%('key','value'))
basicconfig 简单 能做的事情相对少
中文的乱码问题
不能同时往文件和屏幕上输出
配置log对象 稍微有点复杂 能做的事情相对多
import logging
logger = logging.getLogger()
fh = logging.FileHandler('log.log',encoding='utf-8')
sh = logging.StreamHandler() # 创建一个屏幕控制对象
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter2 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s [line:%(lineno)d] : %(message)s')
# 文件操作符 和 格式关联
fh.setFormatter(formatter)
sh.setFormatter(formatter2)
# logger 对象 和 文件操作符 关联
logger.addHandler(fh)
logger.addHandler(sh)
logging.debug('debug message') 低级别的 排错信息
logging.info('info message') 正常信息
logging.warning('警告错误') 警告信息
logging.error('error message') 错误信息
logging.critical('critical message') 高级别的 严重错误信息
程序的充分解耦
让程序变得高可定制
zabbix
logging
有5种级别的日志记录模式 :
两种配置方式:basicconfig 、log对象
django框架