使用Python解析阿里云CDN日志
admin
2023-07-31 01:45:57
0

某些原因,一开始没有设计网站的统计模块
如今需要加上,只能借助于百度统计或者阿里云的cdn日志文件,阿里云cdn的日志文件是web的访问信息

log

[9/Mar/2016:00:00:16 +0800] 222.171.7.89 - 62113 \"http://cloud.insta360.com/post/5e7b029d8ed7e3c4b23006a71bab73c8?e=true&m=true\" \"GET http://cloud.insta360.com/public/media/mp4/5e7b029d8ed7e3c4b23006a71bab73c8_960x480.mp4\" 206 509 20516390 HIT \"Mozilla/5.0 (iPhone; CPU iPhone OS 8_4_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H321 NewsApp/5.3.2\" \"video/mp4\"

fileds

  • 时间

  • 访问IP

  • 回源IP

  • responsetime

  • referer

  • method

  • 访问URL

  • httpcode

  • requestsize

  • responsesize

  • cache命中状态

  • UA头

  • 文件类型

re

# 将单条记录转换为Dict对象
def line2dict(line):
    # Snippet, thanks to http://www.seehuhn.de/blog/52
    parts = [
        r\'\\[(?P

script

AliyunLog.py

# coding=utf-8

import fileinput
import re
import os

try:
    import simplejson as json
except ImportError:
    import json


# 读取输入文件并返回Dict对象
def readfile(file):
    filecontent = {}
    index = 0
    statinfo = os.stat(file)

    # just a guestimate. I believe a single entry contains atleast 150 chars
    if statinfo.st_size < 150:
        print \"Not a valid log file. It does not have enough data\"
    else:
        for line in fileinput.input(file):
            index = index + 1
            if line != \"\\n\":  # don\'t read newlines
                filecontent[index] = line2dict(line)

        fileinput.close()
    return filecontent


# 将单条记录转换为Dict对象
def line2dict(line):
    # Snippet, thanks to http://www.seehuhn.de/blog/52
    parts = [
        r\'\\[(?P

main.py

#!/usr/bin/env python
# coding=utf-8

import sys
from AliyunLog import *

def main():
    if len(sys.argv) < 3:
        print \"Incorrect Syntax. Usage: python main.py -f \"
        sys.exit(2)
    elif sys.argv[1] != \"-f\":
        print \"Invalid switch \'\" + sys.argv[1] + \"\'\"
        sys.exit(2)
    elif os.path.isfile(sys.argv[2]) == False:
        print \"File does not exist\"
        sys.exit(2)

    print toJson(sys.argv[2])


if __name__ == \"__main__\":
    main()

result

run script

python main.py -f data

terminal


{
    \"6432\": {
        \"res_time\": \"1728\", 
        \"res_ip\": \"118.114.213.118\", 
        \"req_size\": \"768\", 
        \"req_url\": \"GET http://cloud.insta360.com/public/media/mp4/f9e4bf15d452440c2884b234854d089c_audio.mp3\", 
        \"origin_ip\": \"-\", 
        \"referer\": \"http://cloud.insta360.com/post/f9e4bf15d452440c2884b234854d089c?m=true&from=timeline&isappinstalled=0\", 
        \"content_type\": \"audio/mpeg\", 
        \"time\": \"9/Mar/2016:00:59:58 +0800\", 
        \"ua\": \"Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13C75 MicroMessenger/6.3.13 NetType/WIFI Language/zh_CN\", 
        \"http_code\": \"206\", 
        \"res_size\": \"5290084\", 
        \"cache_status\": \"HIT\"
    },
    ...
}

more

  • 参考了github上apache log的解析方法

  • 原文地址:parse-aliyun-cdn-log-file-with-python

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...