Python tail 实现S3 动态上传
admin
2023-04-11 12:03:47
0

本文源码转至:https://github.com/kasun/python-tail 感谢作者, 稍加修改,实现了文件动态上传的功能

一: 主体执行部分
[root@linux219 base]# cat vsftp.py
#!/usr/bin/env python
#-*-coding:UTF-8-*-
"""
@Item   :  cheetah v1.0
@Author :  william
@Group  :  System YunWei
@Date   :  2015-01-28
@E-mail :  swq.499809608@163.com
@Funtion:
    
"""
import json,sys,time,os
import time
import hmac
import hashlib
import requests
import re
import tail
Dfile = "/data/base/vsftp"
Vfile = "/var/log/vsftpd.log"
Nfile = "/data/base/vsftp/n.log"
def s3Vsftp():
    os.system("cp -fr %s %s "%(Vfile,Nfile))
    os.system("echo ''>%s "%Vfile)
    fp = open(Nfile,'r').readlines()
    for x in fp:
        if  re.search("CHMOD",x) or re.search("UPLOAD",x):
            x =  x.strip()
            x =  x.split('"')[3]
            nx = x.split()[0]
            
            if os.path.isfile("/data/cifs"+nx):
                os.system("s3cmd --acl-public put /data/cifs%s s3://swq499809608.aws.com%s" %(nx,nx))
            elif re.search('zip',nx):
                try:
                    os.system("s3cmd --acl-public put %s s3://swq499809608.aws.com%s" %(nx,nx.split('imguser')[1]))
                except:
                    pass
    os.system("mv  %s %s/%s.file"%(Nfile,Dfile,time.strftime("%F-%H-%M")))
def  work():
    while True:
        t = tail.Tail(Vfile)
        print t.follow()
if __name__ == "__main__":
    sc = work()
    
    
二: tail. 实现部分
源码转至: https://github.com/kasun/python-tail
[root@linux219 base]# cat tail.py
#!/usr/bin/env python
'''
Python-Tail - Unix tail follow implementation in Python.
python-tail can be used to monitor changes to a file.
Example:
    import tail
    # Create a tail instance
    t = tail.Tail('file-to-be-followed')
    # Register a callback function to be called when a new line is found in the followed file.
    # If no callback function is registerd, new lines would be printed to standard out.
    t.register_callback(callback_function)
    # Follow the file with 5 seconds as sleep time between iterations.
    # If sleep time is not provided 1 second is used as the default time.
    t.follow(s=5) '''
# Author - Kasun Herath 
# Source - https://github.com/kasun/python-tail
import os
import sys
import time
import re
class Tail(object):
    ''' Represents a tail command. '''
    def __init__(self, tailed_file):
        ''' Initiate a Tail instance.
            Check for file validity, assigns callback function to standard out.
            
            Arguments:
                tailed_file - File to be followed. '''
        self.check_file_validity(tailed_file)
        self.tailed_file = tailed_file
        self.callback = sys.stdout.write
    def follow(self, s=1):
        ''' Do a tail follow. If a callback function is registered it is called with every new line.
        Else printed to standard out.
    
        Arguments:
            s - Number of seconds to wait between each iteration; Defaults to 1. '''
        with open(self.tailed_file) as file_:
            # Go to the end of file
            file_.seek(0,2)
            while True:
                curr_position = file_.tell()
                line = file_.readline()
                if not line:
                    file_.seek(curr_position)
                    time.sleep(s)
                else:
                    #self.callback(line)
                    if  re.search("CHMOD",line) or re.search("UPLOAD",line):
                        #修改主体部分
                        x =  line.strip()
                        x =  x.split('"')[3]
                        nx = x.split()[0]
                        # commad s3cmd put file
              
                        if os.path.isfile("/data/cifs"+nx):
                            print "/data/cifs"+nx
                            os.system("s3cmd --acl-public put /data/cifs%s s3://swq499809608.aws.com%s" %(nx,nx))
                            print ("s3cmd --acl-public put /data/cifs%s s3://swq499809608.aws.com%s" %(nx,nx))
                        elif re.search('zip',nx):
                            try:
                                os.system("s3cmd --acl-public put %s s3://swq499809608.aws.com%s" %(nx,nx.split('imguser')[1]))
                            except:
                                pass
                        else:
                            print 'no'
                        #主体结束部分
    def register_callback(self, func):
        ''' Overrides default callback function to provided function. '''
        self.callback = func
    def check_file_validity(self, file_):
        ''' Check whether the a given file exists, readable and is a file '''
        if not os.access(file_, os.F_OK):
            raise TailError("File '%s' does not exist" % (file_))
        if not os.access(file_, os.R_OK):
            raise TailError("File '%s' not readable" % (file_))
        if os.path.isdir(file_):
            raise TailError("File '%s' is a directory" % (file_))
class TailError(Exception):
    def __init__(self, msg):
        self.message = msg
    def __str__(self):
        return 'yes',self.message.split('')

相关内容

热门资讯

徐巧芯剖析赖清德:最怕自己丢掉... 海峡导报综合报道 大罢免投票一周年纪念活动26日举行,台北市长蒋万安出席并怒轰,民进党一年过后仍没有...
中国红十字会总会紧急向广东调拨... 记者从中国红十字会总会了解到,针对台风“红霞”给广东造成的灾情,中国红十字会总会启动四级应急响应,紧...
这些少年,正被“毒蛋”围猎 李梦抽了几口电子烟,吐出了白色的烟雾,有一股浓烈的水果香味。随后,她看着自己握电子烟的手,跟身边人说...
蒋万安号召“倒阁”,卢秀燕认为... 海峡导报综合报道 台北市长蒋万安27日抛出震撼议题,表示多名民代向他提议推动“倒阁”,要求台行政机构...
特朗普将在白宫会见两位重要客人 俄乌战争仍未结束,美伊冲突又有升级风险。在此背景下,美国总统特朗普将在白宫接见乌克兰和以色列领导人。...
新租客打扫卧室柜子上掉落4万元... 近日,莲前西路某小区内,张倩签下一套房子的租赁合同。当天上午,她带着12岁的大女儿周馨怡和11岁的小...
六部门通告:禁止涉军队退役报废... 关于禁止涉军队退役报废装备销售活动的通告近年来,部分经营者在线上线下公开销售涉及我军退役报废装备(指...
九寨沟景区发生泥石流,部分道路... 7月26日,九寨沟风景名胜区管理局发布泥石流灾害通报:2026年7月26日15时50分,九寨沟景区局...
空调26度不凉是什么原因 从空调内部元器件来说,不凉的原因有可能是温度传感器出现故障了,也有可能是空调压缩机或者铜管出现问题,...
鲸鸿动能携手香港国际机场、鸿蒙...   2026 年 7 月 25 日,十余位鸿蒙车主组成的车队从珠海出发,经港珠澳大桥通关抵达香港国际...