-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment2.py
147 lines (112 loc) · 4.78 KB
/
assignment2.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import enum
from itertools import count
from posixpath import split
import numpy as np
import datetime
def uniqueList(list):
x = np.array(list)
return len(np.unique(x)), np.unique(x)
# create a hashtable for all webservers -> no of occurances.
# find the max occurance from the mapping
def mostFreq(list, uniqlist):
freq = {}
for i in list:
if (i in freq):
freq[i] += 1
else:
freq[i] = 1
maxOccur = max(freq, key=freq.get)
return maxOccur
def readLeftFiles():
ipList = []
accessTimeMax = []
with open("intersec/left.txt") as file:
leftList = file.readlines()
for count, line in enumerate(leftList):
# Check for unique IP addresses
split1 = line.split(" ")
currentIp = split1[0]
ipList.append(currentIp)
# break the datetime object from left.txt
timeObj = split1[1].replace("[", "").replace("]", "")
currentDate = datetime.datetime.strptime(timeObj, '%Y-%m-%dT%H:%M:%S%z')
weekDay = int(currentDate.strftime("%w"))
onlyHour = int(currentDate.strftime("%H"))
onlyMin = int(currentDate.strftime("%M"))
onlySec = int(currentDate.strftime("%S"))
# Monday to Friday 8:15 pm (taking 30 sec interval on both sides)
if (weekDay < 5 and onlyHour == 20):
if (onlyMin == 14 and onlySec > 30):
accessTimeMax.append(currentIp)
elif (onlyMin == 15 and onlySec < 30):
accessTimeMax.append(currentIp)
uniqIpCounter = uniqueList(ipList)[0]
ipListForMax = uniqueList(accessTimeMax)
return count + 1, uniqIpCounter, ipListForMax
def findIpFromLeft(list):
with open("intersec/left.txt") as file:
leftList = file
ipPool = []
for count, line in enumerate(leftList):
split1 = line.split(" ")
timeObj = split1[1].replace("[", "").replace("]", "")
if (timeObj in list):
ipPool.append(line)
uniqIp = uniqueList(ipPool)[1]
print(uniqIp)
def readRightFiles():
webServerList = []
timeOfAccess = []
with open("intersec/right.txt", 'r') as file:
rightList = file
for count, line in enumerate(rightList):
split1 = line.split("//")[1]
split2 = split1.split('"')[0]
webServerList.append(split2)
time = line.split(" ")[0].replace("[", "").replace("]", "")
currentDate = datetime.datetime.strptime(time, '%Y-%m-%dT%H:%M:%S%z')
onlyHour = int(currentDate.strftime("%H"))
onlyMin = int(currentDate.strftime("%M"))
onlySec = int(currentDate.strftime("%S"))
if (split2 == "www.tv-movie.de"):
if (onlyHour == 21 and onlyMin == 59 and onlySec >= 0):
for i in range(1, 6):
if (i == 1):
timeOfAccess.append(time)
chng = time.replace(str(onlySec), str(onlySec - i))
timeOfAccess.append(chng)
elif (onlyHour == 22 and onlyMin == 1):
for i in range(1, 6):
if (i == 1):
timeOfAccess.append(time)
chng = time.replace(str(onlySec), str(onlySec - i))
timeOfAccess.append(chng)
elif (onlyHour == 22 and onlyMin == 0 and onlySec <= 59):
for i in range(1, 6):
if (i == 1):
timeOfAccess.append(time)
chng = time.replace(str(onlySec), str(onlySec - i))
timeOfAccess.append(chng)
findIpFromLeft(timeOfAccess)
totalWebServers = uniqueList(webServerList)[0]
# print(timeOfAccess)
mostVisitedServer = mostFreq(webServerList, totalWebServers)
return count + 1, totalWebServers, mostVisitedServer
def main():
# number of entries in left.txt & right.txt
result_left = readLeftFiles()
result_right = readRightFiles()
print("Result left: ", result_left[0])
print("Result right: ", result_right[0])
uniqIpCount = result_left[1]
print("Number of unique users: ", uniqIpCount)
noOfServes = result_right[1]
print("Number of accessed webservers: ", noOfServes)
mostFreqServer = result_right[2]
print("Most visited webserver: ", mostFreqServer)
totalCandidateIP = result_left[2][0]
candidateList = result_left[2][1]
print("Total candidate IP: ", totalCandidateIP)
print("Candidate IP List: ", candidateList)
if __name__ == '__main__':
main()