Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
ftp log summarising problem Posted by Paul37 on 17 Jan 2003 at 4:10 AM
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)



Report
Re: ftp log summarising problem Posted by infidel on 17 Jan 2003 at 7:51 AM
This message was edited by Moderator at 2003-1-17 7:52:19

: 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:

#!/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)



infidel



Report
Re: ftp log summarising problem Posted by Paul37 on 17 Jan 2003 at 9:33 AM
Thanks - I will try this.
: This message was edited by Moderator at 2003-1-17 7:52:19

: : 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:
:
:
: #!/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)
: 

:
:
: infidel
:
:
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.