2018-06-27-Python全栈开发day23-logging、configparser、hashlib模块

1.logging模块

  logging模块就是日志模块,用来记录程序运行的状态。

  其中有五个级别:

  •   debug,
  •   info,
  •   warning,
  •   error,
  •   critical

 


logging.debug('debug message')#
logging.info('info message')
logging.warning('warn message')#表明发生了一些意外,或者不久的将来会发生问题(如‘磁盘满了’)。软件还是在正常工作。
logging.error('error message')#由于更严重的问题,软件已不能执行一些功能了。
logging.critical('critical message')#严重错误,表明软件已不能继续运行了。

用来输出日志内容,自身可以对日志内容根据自己的需要进行定制
WARNING:root:warn message
ERROR:root:error message
CRITICAL:root:critical message#在输出的过程中,root为目前使用的用户

1.1 调整显示参数

参数名称    描述
filename    指定日志输出目标文件的文件名,指定该设置项后日志信心就不会被输出到控制台了
filemode    指定日志文件的打开模式,默认为'a'。需要注意的是,该选项要在filename指定时才有效
format    指定日志格式字符串,即指定日志输出时所包含的字段信息以及它们的顺序。logging模块定义的格式字段下面会列出。
datefmt    指定日期/时间格式。需要注意的是,该选项要在format中包含时间字段%(asctime)s时才有效
level    指定日志器的日志级别
stream    指定日志输出目标stream,如sys.stdout、sys.stderr以及网络stream。需要说明的是,stream和filename不能同时提供,否则会引发 ValueError异常
style    Python 3.2中新添加的配置项。指定format格式字符串的风格,可取值为'%''{''$',默认为'%'
handlers    Python 3.3中新添加的配置项。该选项如果被指定,它应该是一个创建了多个Handler的可迭代对象,这些handler将会被添加到root logger。需要说明的是:filename、stream和handlers这三个配置项只能有一个存在,不能同时出现2个或3个,否则会引发ValueError异常。

   1.1 level----basicConfig

      

logging.basicConfig(filename='logger.log', level=logging.INFO)
#使用basicconfig方法,来对显示内容进行显示,如果有filename,则是将日志内容存到相应的文件中,否则是输出到屏幕上,
  #level是最低显示什么级别的信息,如果想过滤掉一些无伤大雅的信息,则可以使用info

  1.2format

    

import logging
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s-%(levelname)s-%(message)s')
logging.debug('debug message')
logging.info('info message')
logging.warning('warn message')
logging.error('error message')
logging.critical('critical message')
2018-07-02 22:42:36,277-DEBUG-debug message
2018-07-02 22:42:36,544-INFO-info message
2018-07-02 22:42:36,544-WARNING-warn message
2018-07-02 22:42:36,544-ERROR-error message
2018-07-02 22:42:36,545-CRITICAL-critical message

format的格式是time-levelname-message

  1.3 logging的四大模块

     

组件名称    对应类名    功能描述
日志器    Logger    提供了应用程序可一直使用的接口
处理器    Handler    将logger创建的日志记录发送到合适的目的输出
过滤器    Filter    提供了更细粒度的控制工具来决定输出哪条日志记录,丢弃哪条日志记录
格式器    Formatter    决定日志记录的最终输出格式

  1.3.1 同时把日志打印到屏幕,并且写入文件

  

import logging

logger=logging.getLogger('abc.txt')
logger.setLevel(logging.DEBUG)#设置日志级别
fh=logging.FileHandler('abc.txt')#将日志输出到文件内
ch=logging.StreamHandler()#输出到屏幕
logger.addHandler(fh)#将方法加入到处理器中
logger.addHandler(ch)
fm=logging.Formatter('%(asctime)s-%(levelname)s-%(message)s')#设置输出格式
fh.setFormatter(fm)#应用格式
ch.setFormatter(fm)
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
2018-07-03 00:05:53,601-DEBUG-debug message
2018-07-03 00:05:53,601-INFO-info message
2018-07-03 00:05:53,601-WARNING-warning message
2018-07-03 00:05:53,601-ERROR-error message
2018-07-03 00:05:53,601-CRITICAL-critical message

  1.3.2 有root且有子用户的时候

      root作为父进程,打印子用户的信息时,会同时打印父进程root的信息

2.configparser模块

  配置文件,是以字典的形式进行存储

  

import configparser
config = configparser.ConfigParser()     #config={}#相当于定义一个空字典
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'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here



with open('example.ini', 'w') as f:
    config.write(f)
[DEFAULT]
serveraliveinterval = 45
compression = yes
compressionlevel = 9

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no

2.1 对配置文件进行查询

    2.1.1查看

        

import configparser
config=configparser.ConfigParser()

config.read('example.ini')
print('bitbucket.org' in config)
print(config['bitbucket.org']['user'])

  2.1.2 对文件的键进行遍历

  2.1.3 增加

    

import configparser
config=configparser.ConfigParser()

config.read('example.ini')
print('bitbucket.org' in config)
print(config['bitbucket.org']['user'])
config.add_section('haibin')
config.set('haibin','k1','value')
with open('example.ini', 'w') as f:
    config.write(f)

  2.1.4 删除

    

import configparser
config=configparser.ConfigParser()

config.read('example.ini')
print('bitbucket.org' in config)
print(config['bitbucket.org']['user'])

config.remove_section('haibin')#删除整个块
config.remove_option('bitbucket.org','user')#删除块下面的key值
with open('example.ini', 'w') as f:
    config.write(f)
    

3.hashlib模块

  进行加密处理的,所有的值都可以进行加密,并且得到一个相同长度的值

    可以对文件进行比对,然后查询是否是同一文件。

    用法

  

import hashlib
obj=hashlib.md5()#以md5的方式进行hash#  为了避免被破解,还可以在md5后面加上一个校验值,减少被破解的几率。#obj=hashlib.md5('sb',encode='utf8')
obj.update('brown'.encode('utf-8'))#使用update方法,开始进行hash,第一个参数是进行hash的值,第二个是编码方式
print(obj.hexdigest())#这个是对所更新的值进行输出
---
6ff47afa5dc7daa42cc705a03fca8a9b

   

猜你喜欢

转载自www.cnblogs.com/hai125698/p/9256581.html