Python tail 实现S3 动态上传
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本文源码转至:https://github.com/kasun/python-tail 感谢作者, 稍加修改,实现了文件动态上传的功能一: 主体执行部分[root@linux219 base]# ca
千家信息网最后更新 2026年11月02日Python tail 实现S3 动态上传
本文源码转至: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,osimport timeimport hmacimport hashlibimport requestsimport reimport tailDfile = "/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-tailimport osimport sysimport timeimport reclass 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('')
部分
主体
源码
动态
作者
功能
文件
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网络科技
什么是科技互联网
互联网科技工作委员会
中国互联网科技工作委员会毛军
金诺互联网科技
高科技与互联网
互联网科技公司官网
互联网科技视频
长亮科技 互联网金融
互联网科技公司介绍
四个字互联网科技公司取名
富国互联网科技创新基金
互联网媒体平台算高科技产业
迪庆互联网科技招生
广州新华互联网科技学校电竞方面
佛山市拉法互联网科技有限公司
云南省自由互联网科技有限公司
互联网金融运营模式中的金融科技
兰石爱特工业互联网科技分公司
互联网的黑科技实验室
互联网科技图片
深圳恒合互联网络科技有限公司
互联网科技有限公司经营范围
金融科技 互联网金融
碳银互联网科技有限公司
互联网媒体平台算高科技产业
深圳市优智互联网科技有限公司
有人说阿里系是互联网科技企业
南京一网一红互联网科技有限公司
互联网科技新闻