-
Notifications
You must be signed in to change notification settings - Fork 0
/
task1_4968_5758.py
83 lines (65 loc) · 2.01 KB
/
task1_4968_5758.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import time
filename = 'access_log'
#Description: Get current time
#Parameters: nil
#Return: string
def getTime():
return time.strftime('%Y-%m-%d %H:%M:%S')
#Description: Add to dictionary
#Parameters: dictionary, string
#Return: string
def addToDict(d,page):
if d.has_key(page):
d[page] = d[page] + 1
else:
d[page] = 1
return d
#Description: Get most visited page in dictionary
#Parameters: dictionary
#Return: tuple (page,count)
def getMostVisited(dict):
page = ''
largest = 0
for p in dict:
num = dict[p]
if num > largest:
largest = num
page = p
return (page,largest)
print 'Program starting time: ', getTime()
#Variables
count = 0
hitsjs = 0
hitsip = 0
largestbytes = 0
largestpage = ''
pages = {}
with open(filename,'r') as f:
for line in f:
count = count + 1
#Get entry
entry = line.split()
#Get page from entry and remove 'http://www.the-associates.co.uk'
page = entry[6].replace('http://www.the-associates.co.uk','')
size = entry[9]
#Check if part of page
if '/assets/js/the-associates.js' in page:
hitsjs = hitsjs + 1
#Check if has ip
if '10.99.99.186' in entry[0]:
hitsip = hitsip + 1
#Check if size is not '-' and if it is larger than current largest
if size.isdigit() and int(size) > largestbytes:
largestbytes = int(size)
largestpage = page
#Add page to dictionary
pages = addToDict(pages,page)
f.close()
#Get most visited page from dicitonary
mostVisited = getMostVisited(pages)
print 'Total entries were ', count
print 'Total hits of \'/assets/js/the-associates.js\' were ', hitsjs
print 'Total hits made by 10.99.99.186 were ', hitsip
print 'The largest page/object was ', largestpage, ' with the size of ', largestbytes, ' bytes'
print 'The highest number of hits was ', mostVisited[1], ' for the page ', mostVisited[0]
print 'Program completed time: ', getTime()