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ě)一個(gè)Python腳本下載嗶哩嗶哩舞蹈區(qū)的所有視頻
- Python實(shí)現(xiàn)一鍵下載視頻腳本
- Python百度指數(shù)獲取腳本下載并保存
相關(guān)文章
使用Python和OpenCV檢測(cè)圖像中的物體并將物體裁剪下來(lái)
這篇文章主要介紹了使用Python和OpenCV檢測(cè)圖像中的物體并將物體裁剪下來(lái),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Django Rest framework解析器和渲染器詳解
這篇文章主要介紹了Django Rest framework解析器和渲染器詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07pytorch中的torch.nn.Conv2d()函數(shù)圖文詳解
這篇文章主要給大家介紹了關(guān)于pytorch中torch.nn.Conv2d()函數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Python中使用語(yǔ)句導(dǎo)入模塊或包的機(jī)制研究
這篇文章主要介紹了Python中使用語(yǔ)句導(dǎo)入模塊或包的機(jī)制研究,同時(shí)對(duì)比了幾種導(dǎo)入包或模塊的語(yǔ)句并簡(jiǎn)要說(shuō)明了這幾種方法之間的幾點(diǎn)優(yōu)劣,需要的朋友可以參考下2015-03-03Python實(shí)現(xiàn)爬取逐浪小說(shuō)的方法
這篇文章主要介紹了Python實(shí)現(xiàn)爬取逐浪小說(shuō)的方法,基于Python的正則匹配功能實(shí)現(xiàn)爬取小說(shuō)頁(yè)面標(biāo)題、鏈接及正文等功能,需要的朋友可以參考下2015-07-07在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法
今天小編就為大家分享一篇在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07python3 sorted 如何實(shí)現(xiàn)自定義排序標(biāo)準(zhǔn)
這篇文章主要介紹了python3 sorted 如何實(shí)現(xiàn)自定義排序標(biāo)準(zhǔn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03