Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ftp log summarising problem

Paul37Paul37 Posts: 5Member
Hi I’m a beginner in Python and looking for help with a problem. I am writing a short sript to display:
1. a simple help message
2. the date, client machine name and file being accessed.
3. the number of times each file is accessed.

I’ve managed the first 2 parts but having trouble with the third. I think that I need to split the file name and increment it by 1, but I’ve struggled to find an easy way to do this. Any ideas?
Thanks
Paul37

Here’s my script so far:

#!/usr/bin/python

import sys, getopt, string

def Usage ():
print "ftpscan [-h][-c]"
sys.exit(0)

def scanner(name, function):
file = open(name,'r')
while 1:
line=file.readline()
if not line: break
function(line)
file.close

def processline(line):
words = string.split(line)
date = words[0:4]
client = words[6]
file = words[8]
day = words[0]
month = words[1]
date = words [2]
time = words[3]
year = words [4]
print day,month,date,time,year,client,file

try:
optlist, list = getopt.getopt(sys.argv[1:],'hc')
except getopt.GetoptError:
Usage()
sys.exit(2)

for opt in optlist:
print opt
if opt[0] == '-h':
print ' help '
sys.exit(0)

if opt[0] == '-c':
print "Under Construction"
sys.exit(0)


filename = 'ftplog.data'
scanner(filename,processline)



Comments

  • infidelinfidel Posts: 2,900Member
    [b][red]This message was edited by Moderator at 2003-1-17 7:52:19[/red][/b][hr]
    : Hi I’m a beginner in Python and looking for help with a problem. I am writing a short sript to display:
    : 1. a simple help message
    : 2. the date, client machine name and file being accessed.
    : 3. the number of times each file is accessed.
    :
    : I’ve managed the first 2 parts but having trouble with the third. I think that I need to split the file name and increment it by 1, but I’ve struggled to find an easy way to do this. Any ideas?
    : Thanks
    : Paul37

    You could use a dictionary to store the counts using the filename as a key. I haven't tested this code, but it might give you an idea:

    [code]
    #!/usr/bin/python

    import sys, getopt, string

    def Usage ():
    print "ftpscan [-h][-c]"
    sys.exit(0)

    def scanner(filename, filecounts, function):
    file = open(name,'r')
    while 1:
    line=file.readline()
    if not line: break
    function(line, filecounts)
    file.close

    def processline(line, filecounts):
    words = string.split(line)
    date = words[0:4]
    client = words[6]
    file = words[8]
    day = words[0]
    month = words[1]
    date = words [2]
    time = words[3]
    year = words [4]
    print day,month,date,time,year,client,file
    if filecounts.has_key(file):
    filecounts[file] += 1
    else:
    filecounts[file] = 1

    try:
    optlist, list = getopt.getopt(sys.argv[1:],'hc')
    except getopt.GetoptError:
    Usage()
    sys.exit(2)

    for opt in optlist:
    print opt
    if opt[0] == '-h':
    print ' help '
    sys.exit(0)

    if opt[0] == '-c':
    print "Under Construction"
    sys.exit(0)

    filecounts = {}
    filename = 'ftplog.data'
    scanner(filename,filecounts,processline)
    [/code]


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]



  • Paul37Paul37 Posts: 5Member
    Thanks - I will try this.
    : [b][red]This message was edited by Moderator at 2003-1-17 7:52:19[/red][/b][hr]
    : : Hi I’m a beginner in Python and looking for help with a problem. I am writing a short sript to display:
    : : 1. a simple help message
    : : 2. the date, client machine name and file being accessed.
    : : 3. the number of times each file is accessed.
    : :
    : : I’ve managed the first 2 parts but having trouble with the third. I think that I need to split the file name and increment it by 1, but I’ve struggled to find an easy way to do this. Any ideas?
    : : Thanks
    : : Paul37
    :
    : You could use a dictionary to store the counts using the filename as a key. I haven't tested this code, but it might give you an idea:
    :
    : [code]
    : #!/usr/bin/python
    :
    : import sys, getopt, string
    :
    : def Usage ():
    : print "ftpscan [-h][-c]"
    : sys.exit(0)
    :
    : def scanner(filename, filecounts, function):
    : file = open(name,'r')
    : while 1:
    : line=file.readline()
    : if not line: break
    : function(line, filecounts)
    : file.close
    :
    : def processline(line, filecounts):
    : words = string.split(line)
    : date = words[0:4]
    : client = words[6]
    : file = words[8]
    : day = words[0]
    : month = words[1]
    : date = words [2]
    : time = words[3]
    : year = words [4]
    : print day,month,date,time,year,client,file
    : if filecounts.has_key(file):
    : filecounts[file] += 1
    : else:
    : filecounts[file] = 1
    :
    : try:
    : optlist, list = getopt.getopt(sys.argv[1:],'hc')
    : except getopt.GetoptError:
    : Usage()
    : sys.exit(2)
    :
    : for opt in optlist:
    : print opt
    : if opt[0] == '-h':
    : print ' help '
    : sys.exit(0)
    :
    : if opt[0] == '-c':
    : print "Under Construction"
    : sys.exit(0)
    :
    : filecounts = {}
    : filename = 'ftplog.data'
    : scanner(filename,filecounts,processline)
    : [/code]
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :
    :
    :

Sign In or Register to comment.