欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Python線程池的實(shí)現(xiàn)淺析

    Python線程池的實(shí)現(xiàn)淺析

    當(dāng)有多個(gè)?IO?密集型的任務(wù)要被處理時(shí),我們自然而然會(huì)想到多線程。而線程池的實(shí)現(xiàn)也很簡(jiǎn)單,因?yàn)?Python?提供了一個(gè)標(biāo)準(zhǔn)庫(kù)?concurrent.futures,已經(jīng)內(nèi)置了對(duì)線程池的支持。所以本篇文章,我們就來(lái)詳細(xì)介紹一下該模塊的用法
    2022-08-08
  • 使用Python和OpenCV檢測(cè)圖像中的物體并將物體裁剪下來(lái)

    使用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-10
  • Python with用法實(shí)例

    Python with用法實(shí)例

    這篇文章主要介紹了Python with用法實(shí)例,本文講解了with語(yǔ)句的幾種使用方法和使用場(chǎng)景,需要的朋友可以參考下
    2015-04-04
  • Django Rest framework解析器和渲染器詳解

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

    這篇文章主要介紹了Django Rest framework解析器和渲染器詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 對(duì)PyQt5中的菜單欄和工具欄實(shí)例詳解

    對(duì)PyQt5中的菜單欄和工具欄實(shí)例詳解

    今天小編就為大家分享一篇對(duì)PyQt5中的菜單欄和工具欄實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • pytorch中的torch.nn.Conv2d()函數(shù)圖文詳解

    pytorch中的torch.nn.Conv2d()函數(shù)圖文詳解

    這篇文章主要給大家介紹了關(guān)于pytorch中torch.nn.Conv2d()函數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Python中使用語(yǔ)句導(dǎo)入模塊或包的機(jī)制研究

    Python中使用語(yǔ)句導(dǎo)入模塊或包的機(jī)制研究

    這篇文章主要介紹了Python中使用語(yǔ)句導(dǎo)入模塊或包的機(jī)制研究,同時(shí)對(duì)比了幾種導(dǎo)入包或模塊的語(yǔ)句并簡(jiǎn)要說(shuō)明了這幾種方法之間的幾點(diǎn)優(yōu)劣,需要的朋友可以參考下
    2015-03-03
  • Python實(shí)現(xiàn)爬取逐浪小說(shuō)的方法

    Python實(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)目程序的操作方法

    今天小編就為大家分享一篇在Pycharm中調(diào)試Django項(xiàng)目程序的操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python3 sorted 如何實(shí)現(xiàn)自定義排序標(biāo)準(zhǔn)

    python3 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

最新評(píng)論