-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
executable file
·142 lines (119 loc) · 3.98 KB
/
utils.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 一些函数
def conv_F12_header(f12, browser):
r = {}
if browser == 'firefox':
for k in f12:
headers = f12[k]
headers= headers['headers']
for d in headers:
for k in d:
if k == 'name' :
key = d[k]
elif k == 'value':
value = d[k]
else:
print('未知条目 %s: %s' % (k, d[k]))
r[key] = value
else:
print('未支持的浏览器')
return r
def conv_F12_data(f12, browser):
r = {}
if browser == 'firefox':
for k in f12:
r = f12[k]
return r
# 返回字符串长度,汉字按两个算
def strlen(txt):
lenTxt = len(txt)
lenTxt_utf8 = len(txt.encode('utf-8'))
size = int((lenTxt_utf8 - lenTxt)/2 + lenTxt)
return size
# 打印抬头
def print_title(msg):
tail = ''
for i in range(0,80-strlen(msg)-6):
tail = tail + '-'
print('----- %s %s' % (msg, tail))
# 对齐打印dict,dict各字段的值必须是字符串
import math
def print_dict_fine(d, slim=False):
max_key_len = 0
#max_value_len = 0
# 找到最大key长
for k in d:
if strlen(k) > max_key_len:
max_key_len = strlen(k)
#if strlen(d[k]) > max_value_len:
# max_value_len = strlen(d[k])
# 根据key长度算出合适的tab_stop,并且根据value内容截取部分
for k in d:
tab_stop = ''
for i in range(int(strlen(k)/8), math.ceil(max_key_len/8)):
tab_stop= tab_stop+'\t'
if slim == False:
if isinstance(d[k], str):
print('%s%s: %s' % (k, tab_stop, d[k]))
else:
print('%s%s: ' % (k, tab_stop), d[k])
else:
if strlen(d[k]) < (80-8*math.ceil(max_key_len/8)):
if isinstance(d[k], str):
print('%s%s: %s' % (k, tab_stop, d[k]))
else:
print('%s%s: ' % (k, tab_stop), d[k])
else:
if isinstance(d[k], str):
print('%s%s: %s ( 尾部省略 ... )' % (k, tab_stop, d[k][0:(80-8*math.ceil(max_key_len/8))]))
else:
print('%s%s: ' % (k, tab_stop), d[k])
print('-------------------------------------------------------------------------------')
# 将数字(字符串)转换为数字
def str2num(s):
try:
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
rs = list(s)
rs.reverse()
b = 1
i = 0
for c in rs:
if c in numbers: # c \in ['0', '9']
i = i+b*int(c)
b = b*10
elif c == '.':
i = i/b
b = 1
else:
raise Exception("Invalid Character!")
return i
except Exception:
print('Can not convert %s to number.' % (s))
return False
def log(s):
import traceback
if not hasattr(log, 'prev_func'):
log.prev_func = 'empty'
stackTuple = traceback.extract_stack(limit=2)[0] # limit=2表示调用者的stack
filename = stackTuple[0].split('/')
linenumber = stackTuple[1]
funcname = stackTuple[2]
if log.prev_func == funcname:
pass
else:
print("\033[1;34m---------- %s/%s() --------------------\033[0;30m" % (filename[-1], funcname))
log.prev_func = funcname
print("[%s]" % (linenumber), s)
def warn(s):
import traceback
if not hasattr(warn, 'prev_func'):
warn.prev_func = 'empty'
stackTuple = traceback.extract_stack(limit=2)[0] # limit=2表示调用者的stack
filename = stackTuple[0].split('/')
linenumber = stackTuple[1]
funcname = stackTuple[2]
if warn.prev_func == funcname:
pass
else:
print("\033[1;31m---------- %s/%s() --------------------\033[0;30m" % (filename[-1], funcname))
warn.prev_func = funcname
print("[%s]" % (linenumber), s)