code
Use a dictionary to count the number of hits by hostname from a proxy log file, where the 7th tab delimited entry per line is the host. KeyError is the exception thrown if a dictionary doesn’t yet contain the key being referred to.
def parselog(infile):
i = 0
fbook = 0
totals = {}
for line in infile.xreadlines():
desthost = line.split("\t")[6]
try:
totals[desthost] += 1
except KeyError:
totals[desthost] = 1
if desthost.find("facebook") > 0:
fbook +=1
i += 1
return i, fbook, totals
Comments are closed.