Python学习笔记-2017.5.4thon学习笔记-2017.8.14
生活随笔
收集整理的這篇文章主要介紹了
Python学习笔记-2017.5.4thon学习笔记-2017.8.14
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#hashlib模塊,用來加密
import hashlib
m = hashlib.md5()#生成一個加密對象
m.update(b"hello")#更新對象,如果有中文,需要.encode(encoding = "utf-8")
print(m.hexdigest())#打印加密后的字符串,MD5加密,16進制格式
m.update(b"it's me")
print(m.hexdigest())#代表hello+it's me一起的加密。
#sha1加密只是把m = hashlib.md5換成了m = hashlib.sha1而已,包括125,256,sha512等
#hmac版本,對創(chuàng)建的key再進行加密,等于雙重加密
import hmac
n = hmac.new(b"12345", b"abcde")#key只能是assic碼,message也只能是assic碼
print(n.hexdigest())#十六進制加密
#解決方法
n2 = hmac.new("你好嗎?".encode(encoding="utf-8"), "abcde".encode(encoding="utf-8"))
print(n2.hexdigest())__Author__ = "Jack"
import subprocess#替代os.system和os.spawn*import logging#日志功能"""
#默認調用是root用戶,不輸出的日志級別可以調整,加上時間
logging.basicConfig(filename="niubintest.log",level=logging.INFO,format='%(asctime)s %(filename)s :%(lineno)s %(levelname)s - %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p')#如上面加入的時間參數(shù),可以加入其他很多信息,如模塊名,哪個文件,哪個函數(shù),多少行打印日志#可以調整寫入日志級別
logging.basicConfig函數(shù)各參數(shù):
filename: 指定日志文件名
filemode: 和file函數(shù)意義相同,指定日志文件的打開模式,'w'或'a'
format: 指定輸出的格式和內容,format可以輸出很多有用信息,如上例所示:
%(levelno)s: 打印日志級別的數(shù)值
%(levelname)s: 打印日志級別名稱
%(pathname)s: 打印當前執(zhí)行程序的路徑,其實就是sys.argv[0]
%(filename)s: 打印當前執(zhí)行程序名
%(funcName)s: 打印日志的當前函數(shù)
%(lineno)d: 打印日志的當前行號
%(asctime)s: 打印日志的時間
%(thread)d: 打印線程ID
%(threadName)s: 打印線程名稱
%(process)d: 打印進程ID
%(message)s: 打印日志信息
datefmt: 指定時間格式,同time.strftime()
level: 設置日志級別,默認為logging.WARNING
stream: 指定將日志的輸出流,可以指定輸出到sys.stderr,sys.stdout或者文件,默認輸出到sys.stderr,當stream和filename同時指定時,stream被忽略
logging.critical("The Server was down")
logging.error("test error")
logging.warning("Another user is using this server")
logging.info("test info")#默認不輸出
logging.debug("test debug")#默認不輸出
"""
#日志類別,5個級別的日志,debug,error,warning,info,critical
#Logger提供了可以直接使用的接口
#handler可以將logger日志發(fā)送到合適的目的輸出,如果需要同時輸出到兩個地方,則需要添加兩個handler,以此類推
#filter決定了設備來輸出那條目錄
#format決定了日志輸出的格式#create logger
logger = logging.getLogger("Jack-log")
logger.setLevel(logging.DEBUG)#添加handler
#屏幕handler
ch = logging.StreamHandler()
ch.setLevel(logging.WARNING)#文件handler
fh = logging.FileHandler("Error.log", encoding="utf-8")
fh.setLevel(logging.ERROR)#定義好日志格式:
FormatFile = logging.Formatter('%(asctime)s %(filename)s :%(lineno)s %(levelname)s - %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p')
FormatCh = logging.Formatter('%(asctime)s %(filename)s %(levelname)s - %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p')#將formater和handler做關聯(lián)
ch.setFormatter(FormatCh)
fh.setFormatter(FormatFile)#將handler加入logger,讓logger往這兩個handler輸出
logger.addHandler(ch)
logger.addHandler(fh)#測試
logger.warning("dddd")
logger.error("eeee")__Author__ = "Jack"
#日志過大切割,Rotating模塊,可以調整文件大小,最多保留多少個,
import logging,time
from logging import handlers
#create logger
logger = logging.getLogger("Jack-log")log_file = "timelog.log"
# fh = handlers.RotatingFileHandler(filename=log_file, maxBytes=10, backupCount=3, encoding="utf-8")#按大小
fh = handlers.TimedRotatingFileHandler(filename=log_file, when="S", interval=5,backupCount=3, encoding="utf-8")#按照時間
FormatFile = logging.Formatter('%(asctime)s %(filename)s :%(lineno)s %(levelname)s - %(message)s',datefmt='%m/%d/%Y %I:%M:%S %p')fh.setFormatter(FormatFile)
logger.addHandler(fh)#測試
logger.warning("dddd")
time.sleep(2)
logger.warning("test2")
time.sleep(2)
logger.warning("test3")
time.sleep(2)
logger.warning("test4")
time.sleep(2)
logger.warning("test5")
time.sleep(2)
logger.warning("test6")
time.sleep(2)
logger.error("test7")
?
轉載于:https://www.cnblogs.com/JackNiu/p/7358277.html
總結
以上是生活随笔為你收集整理的Python学习笔记-2017.5.4thon学习笔记-2017.8.14的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [翻译]NUnit--前言(一)
- 下一篇: sigaction函数学习