Python学习笔记-2017.5.4thon学习笔记-2017.8.14
#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版本,对创建的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')
#如上面加入的时间参数,可以加入其他很多信息,如模块名,哪个文件,哪个函数,多少行打印日志
#可以调整写入日志级别
logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(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日志发送到合适的目的输出,如果需要同时输出到两个地方,则需要添加两个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做关联
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")
万古青天一株莲

浙公网安备 33010602011771号