广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python tail 实现S3 动态上
  • 659
分享到

Python tail 实现S3 动态上

动态Pythontail 2023-01-31 07:01:03 659人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

本文源码转至:https://GitHub.com/kasun/python-tail 感谢作者, 稍加修改,实现了文件动态上传的功能一: 主体执行部分 [root@linux219 base]# cat vsftp.py #!/usr/b

本文源码转至: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 <kasunh01 at gmail.com>
# 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('')

--结束END--

本文标题: Python tail 实现S3 动态上

本文链接: https://www.lsjlt.com/news/191198.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python tail 实现S3 动态上
    本文源码转至:https://github.com/kasun/python-tail 感谢作者, 稍加修改,实现了文件动态上传的功能一: 主体执行部分 [root@linux219 base]# cat vsftp.py #!/usr/b...
    99+
    2023-01-31
    动态 Python tail
  • 怎么使用Python实现tail
    本篇内容介绍了“怎么使用Python实现tail”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!1.第一版--从文件尾部读取实时数据主要思路是...
    99+
    2023-07-05
  • python实现tail -f命令功能
    #!/usr/bin/env python #!encoding:utf-8 ''' Python-Tail - Unix tail follow implementation in Python. python-tail can be...
    99+
    2023-01-31
    命令 功能 python
  • 使用Python实现tail的示例代码
    目录前记1.第一版--从文件尾部读取实时数据2.第二版--实现tail -f3.第三版--优雅的读取输出日志文件前记 tail是一个常用的Linux命令, 它可以打印文件的后面n行数...
    99+
    2023-03-01
    Python实现tail Python tail
  • python+selenium实现动态爬
    应用实例可以参考博客中的12306自动抢票应用 https://www.cnblogs.com/mumengyun/p/10001109.html 动态网页数据抓取 什么是AJAX: AJAX(Asynchronouse JavaSc...
    99+
    2023-01-30
    动态 python selenium
  • Mybatis在注解上如何实现动态SQL
    目录在注解上实现动态SQL注解的动态语句支持以下注解方式动态sql写法和注意事项判断字符串为空串 用单引号大于等于用小于等于用在注解上实现动态SQL 使用Mybatis注解实现sql...
    99+
    2022-11-13
  • vue+el-upload实现多文件动态上传
    vue+el-upload多文件动态上传,供大家参考,具体内容如下 使用场景 点击【添加/删除】,可动态增加/删除行数,并为每行上传附件,附件暂存前端,点击【上传】可以将所有附件和部...
    99+
    2022-11-12
  • Mybatis在注解上怎么实现动态SQL
    本文小编为大家详细介绍“Mybatis在注解上怎么实现动态SQL”,内容详细,步骤清晰,细节处理妥当,希望这篇“Mybatis在注解上怎么实现动态SQL”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。在注解上实现动...
    99+
    2023-07-02
  • Python实战之实现获取动态图表
    目录前言开发工具环境搭建百度指数微博指数结果展示前言 利用Python实现获取动态图表,废话不多说~ 让我们愉快地开始吧~ 开发工具 Python版本: 3.6.4 相关模块: re...
    99+
    2022-11-12
  • python实现跨年烟花动态效果
    目录Pygame 绘制烟花的基本原理用Python和Tkinter设计烟花将烟花绽放转译成代码使用Tkinter模拟朋友们,有多久没放烟花了?今年你所在的地方允许放烟花么?既然我们不...
    99+
    2023-01-03
    python实现跨年烟花 python实现烟花效果 python烟花
  • Python粒子烟花动态效果实现
    目录效果展示实现代码剩下代码跨年倒计时还有18天?我已经开始整烟花了,虽然不是很好看吧,但是也能将就看看 这个的背景图,音乐,还有文字都是可以自己修改的哦 效果展示 依次导入本次...
    99+
    2023-01-03
    Python粒子烟花 Python动态烟花 Python烟花
  • Python如何实现炫酷的动态图
    这篇文章主要为大家展示了“Python如何实现炫酷的动态图”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Python如何实现炫酷的动态图”这篇文章吧。启动如果你还没安装 Plotly,只需在你的...
    99+
    2023-06-28
  • python多线程实现动态图绘制
    目录一、背景二、步骤1、使用matplotlib绘制动态图2、创建一个线程用于更新数据三、代码框架一、背景 有些情况下,我们面对实时更新的数据,希望能够在一个窗口中可视化出来,并且能...
    99+
    2022-11-10
  • Python实现动态柱状图的绘制
    目录一.基础柱状图二.基础时间线柱状图三.GDP动态柱状图绘制四.完整代码一.基础柱状图 如图 演示 from pyecharts.charts import Bar from p...
    99+
    2022-12-29
    Python绘制动态柱状图 Python动态柱状图 Python 柱状图
  • Python实现日历壁纸动态标记
    迁自QQ空间 2014-08-08背景可能这个标题不够明确到底要实现什么功能,下面详细介绍一下。由于windows系统任务栏的日期只有年、月、日,对于我来说,偶尔想看看农历,所以每次都要去问度娘。后来发现一个不错的办法,就是找一个带日历(包...
    99+
    2023-01-31
    标记 日历 壁纸
  • 怎么用Python实现动态条形图
    这篇文章主要介绍“怎么用Python实现动态条形图”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用Python实现动态条形图”文章能帮助大家解决问题。目前,官方的API文档只提供了一个条形图的源...
    99+
    2023-07-05
  • Python如何实现获取动态图表
    本篇内容介绍了“Python如何实现获取动态图表”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!开发工具Python版本: 3.6.4相关模块...
    99+
    2023-06-22
  • Python编程实现超炫动态排序图
    目录用 python 制作超燃动态排序视频1,数据预处理2,图表绘制3,制作的图表转化为视频、动画用 python 制作超燃动态排序视频 在开始之前,先贴张图,之前网上一段时间下面这...
    99+
    2022-11-12
  • Python实现动态绘图的示例详解
    目录示例FuncAnimation三维情况示例 matplotlib中的animation提供了动态绘图功能,下面列举一个最简单的动态绘制三角函数的例子,来初步演示一下。 impor...
    99+
    2023-05-19
    Python实现动态绘图 Python动态绘图 Python绘图
  • Flink中动态表上的连续查询怎么实现
    这篇文章主要介绍了Flink中动态表上的连续查询怎么实现,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。用SQL分析数据流越来越多的公司在采用流处理技术,并将现有的批处理应用程...
    99+
    2023-06-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作