reading ftp log file..

Stefan Schwarzer s.schwarzer at ndh.net
Sun Feb 3 08:40:51 EST 2002


Hello Andrew

Andrew Replogle wrote:
> I'm trying to write a python script that watches an ftp log file.

What kind of "watching" do you mean? Runs your script continously or
do you start it in certain intervals via a cronjob (or what else)?

Please tell us a bit more about your application.

> Based on someone logging in and successfully transmitting a file it will use
> the smptlib to generate somewhat of a dynamic mail as an "upload
> confirmation".  But I'm so new to python and programming in general that
> this is proving very difficult.
> 
> this is what I have so far..
> -----------------------------------
> import os
> import sys
> import smtplib
> 
> ServerPath = 'c:\\Program Files\\G6 FTP Server\\'
> logExists = os.path.isfile(ServerPath + 'FTP.log')
> line = "230"
> #print logExists
> 
> log = open(ServerPath + 'FTP.log')
> lines = log.read()
> for line in lines:
>  print "someone logged in"
>  break
> -------------------------------------
> 
> the "230" you see is the code the ftp server puts in the log if X user

You can check a line whether it starts with "230" by writing

if line.startswith("230"):
    ...

> successfully logs in.. I'm trying to search for that in the file and if it
> exists then go on with what the account is & what file they transfered.

How does a complete line in the logfile look like?

> it always prints "someone logges in" even if I change 'line'  to a bazillion
> zeros..

You use read() to access your logfile which slurps the whole file into
a single string. So what your loop above actually does (if you omit the
break statement) is to iterate over each single character:

>>> contents = 'a string'
>>> for char in contents:
...     print char
...
a

s
t
r
i
n
g
 

Stefan



More information about the Python-list mailing list