返回顶部

Python收集Json格式日志的状态码

Python收集Json格式日志的状态码

Python2

#!/usr/bin/env python
#coding:utf-8
status_200= []
status_403= []
with open("/apps/nginx-1.18.0/logs/access_json.log") as f:
    for line in f.readlines():
        line = eval(line)
        if line.get("status") == "200":
            status_200.append(line.get)
        elif line.get("status") == "403":
            status_403.append(line.get)
        else:
            print("状态码 ERROR")
        print(line.get("clientip"))
f.close()
print "状态码200的有--:",len(status_200)
print "状态码403的有--:",len(status_403)

执行脚本

#python log.py
...
10.0.0.107
10.0.0.127
10.0.0.117
10.0.0.127
10.0.0.167
状态码200的有--: 2057
状态码403的有--: 10

Python3

#!/usr/bin/env python3
#coding:utf-8
status_200= []
status_403= []
with open("/apps/nginx-1.18.0/logs/access_json.log") as f:
for line in f.readlines():
line = eval(line)
if line.get("status") == "200":
status_200.append(line.get)
elif line.get("status") == "403":
status_403.append(line.get)
else:
print("状态码 ERROR")
print((line.get("clientip")))
f.close()
print ("状态码200的有--:",len(status_200))
print ("状态码403的有--:",len(status_403))

执行脚本

#python3 log.py
...
10.0.0.127
10.0.0.127
10.0.0.127
10.0.0.127
10.0.0.127
10.0.0.127
状态码200的有--: 2057
状态码403的有--: 0

python 2 3转换

#转换python2语法到python3
#pip3 install 2to3
#2to3 -w log.py

 

posted @ 2022-03-16 15:46  九尾cat  阅读(78)  评论(0编辑  收藏  举报