當(dāng)前位置:
首頁(yè) >
Python学习笔记-2017.5.4thon学习笔记-2017.8.14
發(fā)布時(shí)間:2025/4/14
34
豆豆
生活随笔
收集整理的這篇文章主要介紹了
Python学习笔记-2017.5.4thon学习笔记-2017.8.14
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#hashlib模塊,用來(lái)加密
import hashlib
m = hashlib.md5()#生成一個(gè)加密對(duì)象
m.update(b"hello")#更新對(duì)象,如果有中文,需要.encode(encoding = "utf-8")
print(m.hexdigest())#打印加密后的字符串,MD5加密,16進(jìn)制格式
m.update(b"it's me")
print(m.hexdigest())#代表hello+it's me一起的加密。
#sha1加密只是把m = hashlib.md5換成了m = hashlib.sha1而已,包括125,256,sha512等
#hmac版本,對(duì)創(chuàng)建的key再進(jìn)行加密,等于雙重加密
import hmac
n = hmac.new(b"12345", b"abcde")#key只能是assic碼,message也只能是assic碼
print(n.hexdigest())#十六進(jìn)制加密
#解決方法
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#日志功能"""
#默認(rèn)調(diào)用是root用戶,不輸出的日志級(jí)別可以調(diào)整,加上時(shí)間
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ù),可以加入其他很多信息,如模塊名,哪個(gè)文件,哪個(gè)函數(shù),多少行打印日志#可以調(diào)整寫(xiě)入日志級(jí)別
logging.basicConfig函數(shù)各參數(shù):
filename: 指定日志文件名
filemode: 和file函數(shù)意義相同,指定日志文件的打開(kāi)模式,'w'或'a'
format: 指定輸出的格式和內(nèi)容,format可以輸出很多有用信息,如上例所示:
%(levelno)s: 打印日志級(jí)別的數(shù)值
%(levelname)s: 打印日志級(jí)別名稱
%(pathname)s: 打印當(dāng)前執(zhí)行程序的路徑,其實(shí)就是sys.argv[0]
%(filename)s: 打印當(dāng)前執(zhí)行程序名
%(funcName)s: 打印日志的當(dāng)前函數(shù)
%(lineno)d: 打印日志的當(dāng)前行號(hào)
%(asctime)s: 打印日志的時(shí)間
%(thread)d: 打印線程ID
%(threadName)s: 打印線程名稱
%(process)d: 打印進(jìn)程ID
%(message)s: 打印日志信息
datefmt: 指定時(shí)間格式,同time.strftime()
level: 設(shè)置日志級(jí)別,默認(rèn)為logging.WARNING
stream: 指定將日志的輸出流,可以指定輸出到sys.stderr,sys.stdout或者文件,默認(rèn)輸出到sys.stderr,當(dāng)stream和filename同時(shí)指定時(shí),stream被忽略
logging.critical("The Server was down")
logging.error("test error")
logging.warning("Another user is using this server")
logging.info("test info")#默認(rèn)不輸出
logging.debug("test debug")#默認(rèn)不輸出
"""
#日志類別,5個(gè)級(jí)別的日志,debug,error,warning,info,critical
#Logger提供了可以直接使用的接口
#handler可以將logger日志發(fā)送到合適的目的輸出,如果需要同時(shí)輸出到兩個(gè)地方,則需要添加兩個(gè)handler,以此類推
#filter決定了設(shè)備來(lái)輸出那條目錄
#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做關(guān)聯(lián)
ch.setFormatter(FormatCh)
fh.setFormatter(FormatFile)#將handler加入logger,讓logger往這兩個(gè)handler輸出
logger.addHandler(ch)
logger.addHandler(fh)#測(cè)試
logger.warning("dddd")
logger.error("eeee")__Author__ = "Jack"
#日志過(guò)大切割,Rotating模塊,可以調(diào)整文件大小,最多保留多少個(gè),
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")#按照時(shí)間
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)#測(cè)試
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")
?
轉(zhuǎn)載于:https://www.cnblogs.com/JackNiu/p/7358277.html
總結(jié)
以上是生活随笔為你收集整理的Python学习笔记-2017.5.4thon学习笔记-2017.8.14的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [翻译]NUnit--前言(一)
- 下一篇: 第二阶段---python基础