Python腳本實(shí)現(xiàn)下載合并SAE日志
由于一些原因,需要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è)置的變量如下
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)代碼如下
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命名
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下就可以了
# 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實(shí)現(xiàn)的飛速中文網(wǎng)小說(shuō)下載腳本
- 編寫(xiě)Python腳本來(lái)實(shí)現(xiàn)最簡(jiǎn)單的FTP下載的教程
- 編寫(xiě)Python腳本批量下載DesktopNexus壁紙的教程
- 利用python寫(xiě)個(gè)下載teahour音頻的小腳本
- 使用python采集腳本之家電子書(shū)資源并自動(dòng)下載到本地的實(shí)例腳本
- Python實(shí)現(xiàn)多線(xiàn)程下載腳本的示例代碼
- 寫(xiě)一個(gè)Python腳本下載嗶哩嗶哩舞蹈區(qū)的所有視頻
- Python實(shí)現(xiàn)一鍵下載視頻腳本
- Python百度指數(shù)獲取腳本下載并保存
相關(guān)文章
如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫(kù)中的全部數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了如何使用python轉(zhuǎn)移mysql數(shù)據(jù)庫(kù)中的全部數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解下2024-11-11
pycharm如何debug for循環(huán)里面的錯(cuò)誤值(推薦)
一般debug時(shí),在for循環(huán)里面的話(huà),需要自己一步一步點(diǎn),如果循環(huán)幾百次那種就比較麻煩,此時(shí)可以采用try except的方式來(lái)解決,這篇文章主要介紹了pycharm如何debug for循環(huán)里面的錯(cuò)誤值,需要的朋友可以參考下2024-07-07
python實(shí)現(xiàn)ip地址的包含關(guān)系判斷
這篇文章主要介紹了python實(shí)現(xiàn)ip地址的包含關(guān)系判斷,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
詳解NumPy中的線(xiàn)性關(guān)系與數(shù)據(jù)修剪壓縮
本文將通過(guò)股票均線(xiàn)計(jì)算的案例來(lái)為大家講解一下NumPy中的線(xiàn)性關(guān)系以及數(shù)據(jù)修剪壓縮的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05
python 成功引入包但無(wú)法正常調(diào)用的解決
這篇文章主要介紹了python 成功引入包但無(wú)法正常調(diào)用的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Python實(shí)現(xiàn)發(fā)送QQ郵件的封裝
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)發(fā)送QQ郵件的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
python 編程之twisted詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了python 編程之twisted詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01

