-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
116 lines (96 loc) · 3.07 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# -*- coding: utf8 -*-
'''
Created on 2013-11-20
@author: samuelchen
'''
import logging, logging.handlers
import sys, hashlib
import os
class LogLevelFilter(object):
def __init__(self, level):
self.__level = level
def filter(self, logRecord):
return logRecord.levelno <= self.__level
def setLogPath(path='p2python.log'):
os.environ['P2PYTHON_LOG'] = path
fh = ch = eh = None
log_path = ''
def getLogger(name='P2Python'):
global fh, ch, eh, log_path
if not log_path and 'P2PYTHON_LOG' in os.environ:
log_path = os.environ['P2PYTHON_LOG']
else:
log_path = 'p2python.log'
setLogPath()
log_level = logging.INFO
if 'P2PYTHON_LOG_LEVEL' in os.environ:
lvl = os.environ['P2PYTHON_LOG_LEVEL'].upper()
if lvl == 'DEBUG' or lvl == 'ALL': log_level = logging.DEBUG
elif lvl == 'ERROR': log_level = logging.ERROR
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# file handler.
if not fh:
fh = logging.handlers.TimedRotatingFileHandler(log_path)
fh.suffix = "%Y%m%d.log"
fh.setLevel(log_level)
fh.setFormatter(formatter)
# console handler
if not ch:
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
ch.addFilter(LogLevelFilter(logging.WARN))
ch.setFormatter(formatter)
# stderr handler
if not eh:
eh = logging.StreamHandler(stream=sys.stderr)
eh.setLevel(logging.ERROR)
eh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)
logger.addHandler(eh)
logger.propagate = False
return logger
log = getLogger()
# import socket, fcntl, struct
# socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#
# def getIPbyInterface(ifname):
# return socket.inet_ntoa(fcntl.ioctl(
# s.fileno(),
# 0x8915, # SIOCGIFADDR
# struct.pack('256s', ifname[:15])
# )[20:24])
# def getLocalIPAddress(ifname=None):
# import socket
# ip = '0.0.0.0'
# if type == 'internal':
# hostname = socket.gethostname()
# ip = socket.gethostbyname(hostname)
# elif type == 'external':
# hostname = socket.gethostname()
# ip = socket.gethostbyname(hostname)
#
# log.debug("local ip address: %s" % ip)
# return ip
def getLocalIPAddress(ifname=None):
import socket
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
return ip
def md5(data):
h = hashlib.md5()
h.update(data.encode('utf8'))
return h.hexdigest()
def md5_file(name):
h = hashlib.md5()
f = open(name, 'rb')
while 1:
data = h.update(f.read(8096))
if not data: break
h.update(data)
f.close()
return h.hexdigest()