[Tutor] Real time reading

Alan Gauld alan.gauld at freenet.co.uk
Sun Dec 5 18:55:59 CET 2004


> send more than 5 mails pr minute (spams) and if so, get Python to
send a
> warningmail to the mailmaster.
>
> How would I in the best way read the log?

Using the standard file methods...

> To open the file and close it
> every second sounds like a bad idea?

It would be but you don't need to, you could read it every
5 minutes, every 15 minutes or even every hour. So long as
the messages are timestamped - and in a log file they usually
are - you can simply record where you got to last time,
search down to that point and read forward from there. In fact,
never mind time stamping, you could just find the position
in the file using tell(), and the do a seek() that would
be much faster...

The frequency that you analyze the file will be governed
by how often you need to notify the administrator - and
he/she can't possibly read messages coming every second!
(in fact in the event of a DoS attack your alerts would
probably lock up the admins console - and make you almost
as unpopular as the attackers!)

Once every 5 minutes is probably a reasonable time interval.
But why not make the number of minutes you check the file a
configurable item, either a startup parameter or an environment
variable, ir even store in a config file (seems to have been a
lot about these this week :-)

> Is there some function to analyze the
> file, and automatically extract additions to the file?

If you store where you got to last trime and use seek()
to get back there you can just use read() (or readlines)
to grab the new bits on one go, then use tell() to see
where the end is and store that (that config file again?)

Analyzing the contents is something you will need to
write but that's straight string manipulation (I assume
it is a text file!)

HTH

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list