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('')

相关内容

热门资讯

终于懂了“新星游牛牛是不是有挂... 有 亲,根据资深记者爆料新星游牛牛是可以开挂的,确实有挂(咨询软件无需打...
我来教教您“飞驰娱乐究竟有挂吗... 您好:飞驰娱乐这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游戏...
【第一资讯】“微乐陕西挖坑到底... 您好:微乐陕西挖坑这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款...
玩家分享攻略“皇豪互娱开挂器?... 网上科普关于“皇豪互娱有没有挂”话题很是火热,小编也是针对皇豪互娱作*弊开挂的方法以及开挂对应的知识...
中南钢铁申请高炉自动调整参数方... 国家知识产权局信息显示,广东中南钢铁股份有限公司申请一项名为“一种高炉自动调整参数的方法及其控制系统...
玩家分享攻略“明星麻将究竟有挂... 家人们!今天小编来为大家解答明星麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很...
原创 固... 在过去一段时间内,固态电池被视为电车的终极方案,然而随着业界对固态电池技术的深入研发却发现,固态电池...
终于了解“欢乐众娱究竟有挂吗?... 您好:欢乐众娱这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游戏...
【第一资讯】“天天福州十三水有... 家人们!今天小编来为大家解答天天福州十三水透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪...
玩家分享攻略“天天乐清麻将怎么... 家人们!今天小编来为大家解答天天乐清麻将透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里...