# -*- coding:utf-8 -*-
'''
Created on 2016年10月28日 @author: zhangsongbin
''' import time
class file_read:
def __init__(self,logfilename):
self._logfilename=logfilename def file_readlines(self,line):
print line # def file_readline(self):
def file_readline(self):
f=open(self._logfilename,'r')
f.seek(0,2) #直接到文件结尾
while True:
offset=f.tell()#得到现在的偏移量
line=f.readline()#得到现在偏移量后面一行的内容,这里如果下一行是空的话,偏移量不会变大;如不是空,偏移量会变大。
if not line:
f.seek(offset)#设置新的偏移量
time.sleep(20)
else:
self.file_readlines(line) f.close() if __name__ == '__main__':
a=file_read('./trace_order.log')
a.file_readline()

一个简单的tailf程序,这个程序后续可以拿来做日志分析,目前还有个缺点就是,这个文件如果被删除了,即使新建一个一样的名字的问题,也得把程序再跑一边,后续会优化。

优化版本如下:

 # -*- coding:utf-8 -*-
'''
Created on 2016年10月28日 @author: zhangsongbin
'''
import os.path
import time
class test(object):
pass
class file_read:
def __init__(self,logfilename):
self._logfilename=logfilename
def get_time(self,_logfilename):#得到ctime的方法,里面做了些判断
if os.path.exists(_logfilename):
return time.ctime(os.path.getctime(_logfilename))
else:
print str(self._logfilename)+'is not exist,after 1s system will check again'
time.sleep(1)
self.get_time(_logfilename)
def file_readlines(self,line):
print line def file_readline(self):
if os.path.exists(self._logfilename):#检测文件是否存在
f=open(self._logfilename,'r')
else:
print str(self._logfilename)+'is not exist,after 1s system will check again'
time.sleep(1)
self.file_readline()#重新调用自己这个方法,再次检测文件 f.seek(0,2) #到文件结尾
#before_ctime=time.ctime(os.path.getctime(self._logfilename)) #得到文件的目前的状态时间
before_ctime=self.get_time(self._logfilename)
while True:
offset=f.tell()#得到现在的偏移量
line=f.readline()#得到现在偏移量后面一行的内容,这里如果下一行是空的话,偏移量不会变大;如不是空,偏移量会变大。
if not line:
#after_ctime=time.ctime(os.path.getctime(self._logfilename)) #得到文件当line是空值的状态时间
after_ctime=self.get_time(self._logfilename)
after_offset=f.tell() #得到当line为空的offset
if offset == after_offset and before_ctime != after_ctime and os.path.exists(self._logfilename): #当文件状态时间不一致时,而offset又每次都一样就表示之前的log文件被删除了,我们要重新打开一次log文件
f.close()
f=open(self._logfilename,'r')
line=f.readline() #log的第一行
self.file_readlines(line) #打印第一行,然后跳过本次循环到while true
before_ctime=self.get_time(self._logfilename) #从新去得到before_ctime
continue
time.sleep(0.1)#这个sleep比较重要,如果log文件生成的速度很快,time值就小点,如果生成时间长就写大点。
else:
self.file_readlines(line)
#before_ctime=time.ctime(os.path.getctime(self._logfilename)) #当line不为空表示log正常在读出来了,重新获取一次log的状态时间
before_ctime=self.get_time(self._logfilename) f.close() if __name__ == '__main__':
a=file_read('./echo_log.log')
a.file_readline()

模拟日志生成的shell脚本:

 #!/bin/sh
n=
while true;
do
echo $(date +%Y-%m-%d\;%H:%M:%S) >> echo_log.log
echo $(date +%Y-%m-%d\;%H:%M:%S)
let n=$n+
sleep 0.5
if (( $n == ));then
rm -rf echo_log.log
n=
fi
done

大家可以测试下,先开shell脚本生成日志,再用优化过后的Python程序读出日志。

python实现tailf的更多相关文章

  1. 利用pyinotify监控文件内容,像tailf命令但比它更强

    Linux的tail/tailf命令使用了内核提供的inotify功能,下面的Python例子也使用inotify实现比tail/tailf更强的监控文件功能. watchfile.py #!/usr ...

  2. python基础—迭代器、生成器

    python基础-迭代器.生成器 1 迭代器定义 迭代的意思是重复做一些事很多次,就像在循环中做的那样. 只要该对象可以实现__iter__方法,就可以进行迭代. 迭代对象调用__iter__方法会返 ...

  3. Python编写守护进程程序

    Python编写守护进程程序思路 1. fork子进程,父进程退出通常,我们执行服务端程序的时候都会通过终端连接到服务器,成功连接后会加载shell环境,终端和shell都是进程,shell进程是终端 ...

  4. python自动化开发-[第三天]-编码,函数,文件操作

    今日概要 - 编码详解 - 文件操作 - 初识函数 一.字符编码 1.代码执行过程 代码-->解释器翻译-->机器码-->执行 2.ASCII ASCII:一个Bytes代表一个字符 ...

  5. python websocket网页实时显示远程服务器日志信息

    功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...

  6. 运维开发:python websocket网页实时显示远程服务器日志信息

    功能:用websocket技术,在运维工具的浏览器上实时显示远程服务器上的日志信息 一般我们在运维工具部署环境的时候,需要实时展现部署过程中的信息,或者在浏览器中实时显示程序日志给开发人员看.你还在用 ...

  7. Django实现web端tailf日志文件

    这是Django Channels系列文章的第二篇,以web端实现tailf的案例讲解Channels的具体使用以及跟Celery的结合 通过上一篇<Django使用Channels实现WebS ...

  8. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  9. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

随机推荐

  1. Java 设计模式实现 不错的引用

    这段时间有兴趣重新温习一下设计模式在Java中的实现,碰巧看到一个不错的设计模式总结,这里引用一下作为参考. 创建型模式: JAVA设计模式-Singleton JAVA设计模式-Factory JA ...

  2. 【转】silverlight 跨域访问

    作者:MIDI  来源:博客园  发布时间:2010-01-01 17:39  阅读:204 次  原文链接   [收藏]    在 Silverlight 使用 WebService .WCF.We ...

  3. 一个linux下socket编程的例子,client连server

    关于socket编程,以下文章写得比较好:http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html 1. accept()函 ...

  4. LeetCode (10): Regular Expression Matching [HARD]

    https://leetcode.com/problems/regular-expression-matching/ [描述] Implement regular expression matchin ...

  5. [LeetCode] Word Break II 解题思路

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  6. C++ 匿名名字空间及静态非成员函数

    在C++中,static有一个感觉被较少提及的用法:修饰非成员函数,这个用法实际是从C语言继承来的.其作用是表明这个函数只在当前编译单元中有效.这就使这个函数的所有引用在编译时就可以全部确定,无需进入 ...

  7. K - Transformation-hdu 4578(多操作混合区间更新)线段树

    题意:有四种操作 1,  区间 [l, r] 的值都加上 C 2,  区间 [l, r] 的值都乘上 C 3,  区间 [l, r] 的值都变为C 4,  求区间 [l, r]所有数的p次方的和 分析 ...

  8. 用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件

    项目中经常需要用到此类型的工具 #pragma warning (disable:4786) #include <iostream> #include <map> #inclu ...

  9. YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM

    YKCW6-BPFPF-BT8C9-7DCTH-QXGWCYQ7PR-QTHDM-HCBCV-9GKGG-TB2TM

  10. Configuring the JA-SIG CAS Client --官方

    1. for Java using Spring Configuration of the CAS Client for Java via Spring IoC will depend heavily ...