码迷,mamicode.com
首页 > 编程语言 > 详细

python内置模块(四)

时间:2016-02-23 16:01:07      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:python   logging   hashlib   

----------------------------接 python内置模块(三)---------------------------

十一、hashlib模块 

        用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

        import hashlib

        

        m = hashlib.md5()

        m.update(b"Hello")

        m.update(b"It‘s me")

        print(m.digest())

        m.update(b"It‘s been a long time since last time we ...")

        

        print(m.digest()) #2进制格式hash

        print(len(m.hexdigest())) #16进制格式hash

        

        # ######## md5 ########

        

        hash = hashlib.md5()

        hash.update(b‘admin‘)

        print(hash.hexdigest())

        

        # ######## sha1 ########

        

        hash = hashlib.sha1()

        hash.update(b‘admin‘)

        print(hash.hexdigest())

        

        # ######## sha256 ########

        

        hash = hashlib.sha256()

        hash.update(b‘admin‘)

        print(hash.hexdigest())

        

        

        # ######## sha384 ########

        

        hash = hashlib.sha384()

        hash.update(b‘admin‘)

        print(hash.hexdigest())

        

        # ######## sha512 ########

        

        hash = hashlib.sha512()

        hash.update(b‘admin‘)

        print(hash.hexdigest())

        

另外python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密

        import hmac

        h = hmac.new(b‘shuoming‘)

        h.update(b‘test‘)

        print(h.hexdigest())

        
十二、logging模块

            很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug()info()warning()error() and critical() 5个级别:


LevelWhen it’s used
DEBUGDetailed information, typically of interest only when diagnosing problems.
INFOConfirmation that things are working as expected.
WARNING

An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.

ERRORDue to a more serious problem, the software has not been able to perform some function.
CRITICALA serious error, indicating that the program itself may be unable to continue running.


        1.将日志信息输出到屏幕终端

        import logging

        logging.warning("user [alex] attempted wrong password more than 3 times")

        logging.critical("server is down")

        2.将日志信息输出到文件中

        import logging

        logging.basicConfig(filename=‘example.log‘,level=logging.INFO)

        logging.debug(‘This message should go to the log file‘)

        logging.info(‘So should this‘)

        logging.warning(‘And this, too‘)

        其中下面这句中的level=loggin.INFO意思是,把日志纪录级别设置为INFO,也就是说,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,在这个例子, 第一条日志是不会被纪录的,如果希望纪录debug的日志,那把日志级别改成DEBUG就行了。

        另外还可以加上时间和日期等:

       logging.basicConfig(filename=‘log.log‘,

              format=‘%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s‘,
              datefmt=‘%Y-%m-%d %H:%M:%S %p‘,
              level=10)

        3.将日志信息同时输出到文件和终端

    import logging

     

    #create logger(全局日志级别信息,优先级最高)

    logger = logging.getLogger(‘TEST-LOG‘)

    logger.setLevel(logging.DEBUG)

     

     

    # create console handler and set level to debug(创建输出终端信息及日志级别)

    ch = logging.StreamHandler()

    ch.setLevel(logging.DEBUG)

     

    # create file handler and set level to warning(创建输出到日志文件信息及日志级别)

    fh = logging.FileHandler("access.log")

    fh.setLevel(logging.WARNING)


    # create formatter (创建日志格式)

    formatter = logging.Formatter(‘%(asctime)s - %(name)s - %(levelname)s - %(message)s‘)

     

    # add formatter to ch and fh(定选日志输出格式)

    ch.setFormatter(formatter)

    fh.setFormatter(formatter)

     

    # add ch and fh to logger(添加输出方式)

    logger.addHandler(ch)

    logger.addHandler(fh)

     

    # ‘application‘ code(日志信息)

    logger.debug(‘debug message‘)

    logger.info(‘info message‘)

    logger.warn(‘warn message‘)

    logger.error(‘error message‘)

    logger.critical(‘critical message‘)

    

本文出自 “纷繁中享受技术的简单喜悦” 博客,请务必保留此出处http://51enjoy.blog.51cto.com/8393791/1744288

python内置模块(四)

标签:python   logging   hashlib   

原文地址:http://51enjoy.blog.51cto.com/8393791/1744288

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!