Python脚本实现下载合并SAE日志

 更新时间:2015年02月10日 13:16:47   投稿:junjie  
这篇文章主要介绍了Python脚本实现下载合并SAE日志,本文讲解了代码编写过程,然后给出了完整代码,需要的朋友可以参考下

由于一些原因,需要SAE上站点的日志文件,从SAE上只能按天下载,下载下来手动处理比较蛋疼,尤其是数量很大的时候。还好SAE提供了API可以批量获得日志文件下载地址,刚刚写了python脚本自动下载和合并这些文件

调用API获得下载地址

文档位置在这里

设置自己的应用和下载参数

请求中需要设置的变量如下

复制代码 代码如下:

api_url = 'http://dloadcenter.sae.sina.com.cn/interapi.php?'
appname = 'xxxxx'
from_date = '20140101'
to_date = '20140116'
url_type = 'http' # http|taskqueue|cron|mail|rdc
url_type2 = 'access' # only when type=http  access|debug|error|warning|notice|resources
secret_key = 'xxxxx'

生成请求地址

请求地址生成方式可以看一下官网的要求:

1.将参数排序
2.生成请求字符串,去掉&
3.附加access_key
4.请求字符串求md5,形成sign
5.把sign增加到请求字符串中

具体实现代码如下

复制代码 代码如下:

params = dict()
params['act'] = 'log'
params['appname'] = appname
params['from'] = from_date
params['to'] = to_date
params['type'] = url_type

if url_type == 'http':
    params['type2'] = url_type2

params = collections.OrderedDict(sorted(params.items()))

request = ''
for k,v in params.iteritems():
    request += k+'='+v+'&'

sign = request.replace('&','')
sign += secret_key

md5 = hashlib.md5()
md5.update(sign)
sign = md5.hexdigest()

request = api_url + request + 'sign=' + sign

if response['errno'] != 0:
    print '[!] '+response['errmsg']
    exit()

print '[#] request success'

下载日志文件

SAE将每天的日志文件都打包成tar.gz的格式,下载保存下来即可,文件名以日期.tar.gz命名

复制代码 代码如下:

log_files = list()

for down_url in response['data']:   
    file_name = re.compile(r'\d{4}-\d{2}-\d{2}').findall(down_url)[0] + '.tar.gz'
    log_files.append(file_name)
    data = urllib2.urlopen(down_url).read()
    with open(file_name, "wb") as file:
        file.write(data)

print '[#] you got %d log files' % len(log_files)

合并文件

合并文件方式用trafile库解压缩每个文件,然后把文件内容附加到access_log下就可以了

复制代码 代码如下:

# compress these files to access_log
access_log = open('access_log','w');

for log_file in log_files:
    tar = tarfile.open(log_file)
    log_name = tar.getnames()[0]
    tar.extract(log_name)
    # save to access_log
    data = open(log_name).read()
    access_log.write(data)
    os.remove(log_name)

print '[#] all file has writen to access_log'

完整代码

复制代码 代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Su Yan <http://yansu.org>
# @Date:   2014-01-17 12:05:19
# @Last Modified by:   Su Yan
# @Last Modified time: 2014-01-17 14:15:41

import os
import collections
import hashlib
import urllib2
import json
import re
import tarfile

# settings
# documents http://sae.sina.com.cn/?m=devcenter&catId=281
api_url = 'http://dloadcenter.sae.sina.com.cn/interapi.php?'
appname = 'yansublog'
from_date = '20140101'
to_date = '20140116'
url_type = 'http' # http|taskqueue|cron|mail|rdc
url_type2 = 'access' # only when type=http  access|debug|error|warning|notice|resources
secret_key = 'zwzim4zhk35i50003kz2lh3hyilz01m03515j0i5'

# encode request
params = dict()
params['act'] = 'log'
params['appname'] = appname
params['from'] = from_date
params['to'] = to_date
params['type'] = url_type

if url_type == 'http':
    params['type2'] = url_type2

params = collections.OrderedDict(sorted(params.items()))

request = ''
for k,v in params.iteritems():
    request += k+'='+v+'&'

sign = request.replace('&','')
sign += secret_key

md5 = hashlib.md5()
md5.update(sign)
sign = md5.hexdigest()

request = api_url + request + 'sign=' + sign

# request api
response = urllib2.urlopen(request).read()
response = json.loads(response)

if response['errno'] != 0:
    print '[!] '+response['errmsg']
    exit()

print '[#] request success'

# download and save files
log_files = list()

for down_url in response['data']:   
    file_name = re.compile(r'\d{4}-\d{2}-\d{2}').findall(down_url)[0] + '.tar.gz'
    log_files.append(file_name)
    data = urllib2.urlopen(down_url).read()
    with open(file_name, "wb") as file:
        file.write(data)

print '[#] you got %d log files' % len(log_files)

# compress these files to access_log
access_log = open('access_log','w');

for log_file in log_files:
    tar = tarfile.open(log_file)
    log_name = tar.getnames()[0]
    tar.extract(log_name)
    # save to access_log
    data = open(log_name).read()
    access_log.write(data)
    os.remove(log_name)

print '[#] all file has writen to access_log'

相关文章

  • Python线程池的实现浅析

    Python线程池的实现浅析

    当有多个 IO 密集型的任务要被处理时,我们自然而然会想到多线程。而线程池的实现也很简单,因为 Python 提供了一个标准库 concurrent.futures,已经内置了对线程池的支持。所以本篇文章,我们就来详细介绍一下该模块的用法
    2022-08-08
  • 使用Python和OpenCV检测图像中的物体并将物体裁剪下来

    使用Python和OpenCV检测图像中的物体并将物体裁剪下来

    这篇文章主要介绍了使用Python和OpenCV检测图像中的物体并将物体裁剪下来,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • Python with用法实例

    Python with用法实例

    这篇文章主要介绍了Python with用法实例,本文讲解了with语句的几种使用方法和使用场景,需要的朋友可以参考下
    2015-04-04
  • Django Rest framework解析器和渲染器详解

    Django Rest framework解析器和渲染器详解

    这篇文章主要介绍了Django Rest framework解析器和渲染器详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 对PyQt5中的菜单栏和工具栏实例详解

    对PyQt5中的菜单栏和工具栏实例详解

    今天小编就为大家分享一篇对PyQt5中的菜单栏和工具栏实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • pytorch中的torch.nn.Conv2d()函数图文详解

    pytorch中的torch.nn.Conv2d()函数图文详解

    这篇文章主要给大家介绍了关于pytorch中torch.nn.Conv2d()函数的相关资料,文中通过实例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-02-02
  • Python中使用语句导入模块或包的机制研究

    Python中使用语句导入模块或包的机制研究

    这篇文章主要介绍了Python中使用语句导入模块或包的机制研究,同时对比了几种导入包或模块的语句并简要说明了这几种方法之间的几点优劣,需要的朋友可以参考下
    2015-03-03
  • Python实现爬取逐浪小说的方法

    Python实现爬取逐浪小说的方法

    这篇文章主要介绍了Python实现爬取逐浪小说的方法,基于Python的正则匹配功能实现爬取小说页面标题、链接及正文等功能,需要的朋友可以参考下
    2015-07-07
  • 在Pycharm中调试Django项目程序的操作方法

    在Pycharm中调试Django项目程序的操作方法

    今天小编就为大家分享一篇在Pycharm中调试Django项目程序的操作方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • python3 sorted 如何实现自定义排序标准

    python3 sorted 如何实现自定义排序标准

    这篇文章主要介绍了python3 sorted 如何实现自定义排序标准,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03

最新评论