[Twisted-Python] Where to start: log reader/analysis
yoann.aubineau at wengo.com
Mon Aug 6 04:57:19 EDT 2007
I wrote a class that follows a file (eg. log file) and provides an iterator
to walk through it. Don't know if it may be of any use for you (or others).
"""Iterate through a file while it is updated.
>>> file = FileFollower("/tmp/testfile")
>>> file.interval = 5
>>> for line in file:
... print line
interval = 1
def __init__(self, filename, interval=None):
self.filename = filename
self.interval = interval or self.interval
self.stat = None
self.offset = 0
self.lines = 
self.running = True
# File following
data = self.readChange()
stat = os.stat(self.filename)
if stat != self.stat:
self.stat = stat
file = open(self.filename)
data = file.read()
self.offset = file.tell()
# Data buffering
def dataReceived(self, data):
lines = data.split(os.linesep)
lines = lines[:-1]
for line in lines:
def lineReceived(self, line):
# Iterator implementation
if not self.lines:
line = self.lines.pop(0)
2007/8/5, Andrew E <andrew at ellerton.net>:
> Dear Twisted Experts (... meant in a nice way :) )
> I'm not sure where to start.
> I need to write a small server that:
> - reads lines in a log file as they are appended
> - reads input from a socket as it becomes available
> - does an analysis of both (like, what time was input received in the
> log, and the output received via the socket)
> - outputs a summary report
> Socket I/O is easy - but I'm not sure how to include file reading ...
> its bound to be easy.
> Any tips?
> Thanks :)
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Twisted-Python