最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python腳本實(shí)現(xiàn)下載合并SAE日志

 更新時(shí)間:2015年02月10日 13:16:47   投稿:junjie  
這篇文章主要介紹了Python腳本實(shí)現(xiàn)下載合并SAE日志,本文講解了代碼編寫(xiě)過(guò)程,然后給出了完整代碼,需要的朋友可以參考下

由于一些原因,需要SAE上站點(diǎn)的日志文件,從SAE上只能按天下載,下載下來(lái)手動(dòng)處理比較蛋疼,尤其是數(shù)量很大的時(shí)候。還好SAE提供了API可以批量獲得日志文件下載地址,剛剛寫(xiě)了python腳本自動(dòng)下載和合并這些文件

調(diào)用API獲得下載地址

文檔位置在這里

設(shè)置自己的應(yīng)用和下載參數(shù)

請(qǐng)求中需要設(shè)置的變量如下

復(fù)制代碼 代碼如下:

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'

生成請(qǐng)求地址

請(qǐng)求地址生成方式可以看一下官網(wǎng)的要求:

1.將參數(shù)排序
2.生成請(qǐng)求字符串,去掉&
3.附加access_key
4.請(qǐng)求字符串求md5,形成sign
5.把sign增加到請(qǐng)求字符串中

具體實(shí)現(xiàn)代碼如下

復(fù)制代碼 代碼如下:

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的格式,下載保存下來(lái)即可,文件名以日期.tar.gz命名

復(fù)制代碼 代碼如下:

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庫(kù)解壓縮每個(gè)文件,然后把文件內(nèi)容附加到access_log下就可以了

復(fù)制代碼 代碼如下:

# 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'

完整代碼

復(fù)制代碼 代碼如下:

#!/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'

相關(guān)文章

最新評(píng)論

澄迈县| 库伦旗| 绩溪县| 四平市| 巩留县| 循化| 招远市| 马鞍山市| 巩义市| 新宁县| 罗田县| 开封市| 犍为县| 永福县| 东安县| 乳山市| 利津县| 安福县| 蒙山县| 六安市| 当涂县| 静安区| 温泉县| 柳林县| 梁河县| 乐至县| 焦作市| 尼勒克县| 吉安县| 天门市| 曲周县| 库车县| 晋宁县| 龙州县| 明水县| 榕江县| 天门市| 库尔勒市| 泸溪县| 湘西| 阳朔县|