----------------------------接 python內置模塊(三)---------------------------html
11、hashlib模塊 python
用於加密相關的操做,3.x裏代替了md5模塊和sha模塊,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法算法
import hashlibapp
m = hashlib.md5()ide
m.update(b"Hello")this
m.update(b"It's me")加密
print(m.digest())spa
m.update(b"It's been a long time since last time we ...")debug
print(m.digest()) #2進制格式hashrest
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())
12、logging模塊
不少程序都有記錄日誌的需求,而且日誌中包含的信息即有正常的程序訪問日誌,還可能有錯誤、警告等信息輸出,python的logging模塊提供了標準的日誌接口,你能夠經過它存儲各類格式的日誌,logging的日誌能夠分爲 debug()
, info()
, warning()
, error()
and critical() 5個級別:
Level | When it’s used |
DEBUG | Detailed information, typically of interest only when diagnosing problems. |
INFO | Confirmation 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. |
ERROR | Due to a more serious problem, the software has not been able to perform some function. |
CRITICAL | A 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')
--------------------------------接 內置模塊(五)------------------------------------