接上一篇的日志格式,我们已经筛选出了所有的IP地址、做法稍有不同。nginx正则把IP写进文件里、还是那句话数据太大我们不能写进列表里边:
1
2
3
4
5
6
7
8
9
10
11
12
|
def
getIP():
f
=
open
(log,
'r'
)
IPdic
=
{}
for
logline
in
f.readlines():
matchs
=
p.match(logline)
if
matchs !
=
None
:
allGroups
=
matchs.groups()
IP
=
allGroups[
0
]
f1
=
open
(
'/tmp/ip.txt'
,
'a'
)
f1.write(
"%s\n"
%
(IP))
f1.close()
f.close()
|
2、调用IP模块进行,操作。可以用Pip安装。模块介绍:https://github.com/lxyu/17monip:
pip install 17monip
3、模块使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@monitor python]
# cat query.py
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import
IP
def
CheckIP():
outip
=
open
(
'/tmp/ip.txt'
,
'r'
)
output
=
open
(
'/tmp/output.txt'
,
'w'
)
for
line
in
outip:
listIP
=
line.strip(
'\n'
)
local
=
IP.find(listIP).strip(
" "
)
if
local !
=
"" :
output.write(local.encode(
'utf-8'
))
output.write(
'\n'
)
output.close()
outip.close()
if
__name__
=
=
"__main__"
:
CheckIP()
|
4、在生成文件,用中文写入字典,标记出来,有个地方需要注意的是,当我们字典使用中文的适合,要用json的方式导出数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@monitor python]
# cat queryadd.py
#!/usr/bin/env python
#coding=utf-8
import
json
def
GetAddress():
pvdic
=
{}
f
=
open
(
'/tmp/output.txt'
,
'r'
)
n
=
0
for
line
in
f:
add
=
line.split()
address
=
add[
1
]
pvdic[address]
=
pvdic.get(address,
0
)
+
1
pvdic
=
sorted
(pvdic.iteritems(),key
=
lambda
c:c[
1
],reverse
=
True
)
return
json.dumps(pvdic,encoding
=
"utf-8"
,ensure_ascii
=
False
)
f.close()
if
__name__
=
=
'__main__'
:
print
GetAddress()
|
5、生成效果:
1
2
|
[root@monitor python]
# python queryadd.py
[[
"广东"
,
86
], [
"河北"
,
81
], [
"河南"
,
78
], [
"山东"
,
43
], [
"浙江"
,
43
], [
"福建"
,
31
], [
"北京"
,
30
], [
"江苏"
,
26
], [
"广西"
,
26
], [
"湖南"
,
23
], [
"四川"
,
21
], [
"上海"
,
20
], [
"天津"
,
16
], [
"安徽"
,
14
], [
"黑龙江"
,
13
], [
"陕西"
,
11
], [
"湖北"
,
8
], [
"江西"
,
5
], [
"云南"
,
3
], [
"吉林"
,
3
], [
"山西"
,
3
], [
"贵州"
,
2
], [
"辽宁"
,
2
], [
"甘肃"
,
1
], [
"内蒙古"
,
1
], [
"重庆"
,
1
], [
"香港"
,
1
]]
|
本文转自 小罗ge11 51CTO博客,原文链接:http://blog.51cto.com/xiaoluoge/1653123,如需转载请自行联系原作者