Here is the output I am expecting
Queue Contents =>
[('PK', '111.202.230.44', '62.82.29.190', [31, 'HTTP', [80, 20], 1562431, 38]), ('PK', '222.57.155.164', '50.168.160.19', [22, 'UDP', [90, 5431], 1662431, 82]), ('PK', '333.230.18.207', '213.217.236.184', [56, 'IRC', [501, 5643], 1762431, 318]), ('PK', '444.221.232.94', '50.168.160.19', [1003, 'TCP', [4657, 4875], 1962431, 428]), ('PK', '555.221.232.94', '50.168.160.19', [236, 'TCP', [7753, 5724], 2062431, 48])]
Is Queue Object => True
Is Queue Object (2) - False
Is Empty Queue => False
Is Empty Queue (2) => True
here is my output
Queue Contents => [('PK', '111.202.230.44', '62.82.29.190', [31, 'HTTP', [80, 20], 1562431, 38]), ('PK', '222.57.155.164', '50.168.160.19', [22, 'UDP', [90, 5431], 1662431, 82]), ('PK', '333.230.18.207', '213.217.236.184', [56, 'IRC', [501, 5643], 1762431, 318]), ('PK', '444.221.232.94', '50.168.160.19', [1003, 'TCP', [4657, 4875], 1962431, 428]), ('PK', '555.221.232.94', '50.168.160.19', [236, 'TCP', [7753, 5724], 2062431, 48])]
Is Queue Object => True
Is Queue Object (2) =True -should be false i am having trouble fixing this
Is Empty Queue => False
Is Empty Queue (2) => True
Input:
111.202.230.44 62.82.29.190 3 HTTP 80 3463 1562431 87
1
2
3 import math
4 import os
5 import random
6 import re
7 import sys
8
9
10
11
12
13
14 def makePacket(srcIP, dstIP, length, prt, sp, dp, sqn, pld):
15 return ("PK", srcIP, dstIP, [length, prt, [sp, dp], sqn, pld])
16
17 def getPacketSrc(pkt):
18 return pkt[1]
19
20 def getPacketDst(pkt):
21 return pkt[2]
22
23 def getPacketDetails(pkt):
24 return pkt[3]
25
26 def isPacket(pkt):
27 return type(pkt[1]) != type([]) and pkt[0] == "PK" and type(pkt) == type(())
28
29 def isEmptyPkt(pkt):
30 return getPacketDetails(pkt) == []
31
32 def getLength(pkt):
33 a = getPacketDetails(pkt)
34 return a[0]
35
36 def getProtocol(pkt):
37 a = getPacketDetails(pkt)
38 return a[1]
39
40 def getSrcPort(pkt):
41 a = getPacketDetails(pkt)
42 b = a[2]
43 return b[0]
44
45 def getDstPort(pkt):
46 a = getPacketDetails(pkt)
47 b = a[2]
48 return b[1]
49
50 def getSqn(pkt):
51 a = getPacketDetails(pkt)
52 return a[3]
53
54 def getPayloadSize(pkt):
55 a = getPacketDetails(pkt)
56 return a[4]
57
58 def flowAverage(pkt_list):
59 payloads = []
60 large_packets = []
61 for pkt in pkt_list:
62 payloads.append(getPayloadSize(pkt))
63 total = sum(payloads)
64 avg = total / len(payloads)
65
66 for pkt in pkt_list:
67 if getPayloadSize(pkt) > avg:
68 large_packets.append(pkt)
69 return large_packets
70
71 def suspPort(pkt):
72 if getSrcPort(pkt) > 500 or getDstPort(pkt) > 500:
73 return True
74 else:
75 return False
76
77 def suspProto(pkt):
78 ProtocolList = ["HTTP","SMTP","UDP","TCP","DHCP"]
79 if getProtocol(pkt) not in ProtocolList:
80 return True
81 else:
82 return False
83
84 def ipBlacklist(pkt):
85 IpBlackList = ["213.217.236.184","444.221.232.94","149.88.83.47","223.70.250.146","169.51.6.136","229.223.169.245"]
86 if getPacketSrc(pkt) in IpBlackList:
87 return True
88 else:
89 return False
90
91 def calScore(pkt):
92 pk1 = makePacket("111.202.230.44","62.82.29.190",31,"HTTP",80,20,1562431,38)
93 pk2 = makePacket("222.57.155.164","50.168.160.19",22,"UDP",90,5431,1662431,82)
94 pk3 = makePacket("333.230.18.207","213.217.236.184",56,"IRC",501,5643,1762431,318)
95 pk4 = makePacket("444.221.232.94","50.168.160.19",1003,"TCP",4657,4875,1962431,428)
96 pk5 = makePacket("555.221.232.94","50.168.160.19",236,"TCP",7753,5724,2062431,48)
97 pkt_list = [pk1, pk2, pk3, pk4,pk5,pkt]
98 a = flowAverage(pkt_list)
99 b = suspProto(pkt)
100 c = suspPort(pkt)
101 d = ipBlacklist(pkt)
102 score = 0
103 if pkt in a:
104 score += 3.56
105 if b == True:
106 score += 2.74
107 if c == True:
108 score += 1.45
109 if d == True:
110 score += 10
111 return score
112
113 def makeScore(pkt_list):
114 Slst = []
115 for x in pkt_list:
116 Slst += [(x, calScore(x))]
117 return ("SCORE", Slst)
118
119 def getScorelstContents(ScoreList):
120 return ScoreList[1]
121
122 def addPacket(ScoreList, pkt):
123 x = calScore(pkt)
124 y = (pkt, x)
125 return getScorelstContents(ScoreList).append(y)
126
127 def getSuspPkts(ScoreList):
128 x = getScorelstContents(ScoreList)
129 suslst = []
130 for y in x:
131 if y[1] > 5:
132 suslst += [(y[0])]
133 return suslst
134
135 def getRegulPkts(ScoreList):
136 x = getScorelstContents(ScoreList)
137 reglst = []
138 for z in x:
139 if z[1] <= 5:
140 reglst += [(z[0])]
141 return reglst
142
143 def isScore(ScoreList):
144 return ScoreList[0] == "SCORE" and type(ScoreList) == type(()) and type(ScoreList[1]) == type([])
145
146 def isEmptyScore(ScoreList):
147 return ScoreList == []
148
149 def makePacketQueue():
150 return ("PQ", [])
151
152 def contentsQ(q):
153 return q[1]
154
155 def frontPacketQ(q):
156 x = contentsQ(q)[0]
157 return x[0]
158
159 def addToPacketQ(pkt,q):
160 x = get_pos(pkt,contentsQ(q))
161 contentsQ(q).insert(x,pkt)
162 return q
163
164
165 def get_pos(pkt,lst):
166 if (lst == []):
167 return 0
168 elif getSqn(pkt) < getSqn(lst[0]):
169 return 0 + get_pos(pkt,[])
170 else:
171 return 1 + get_pos(pkt,lst[1:])
172
173 def removeFromPacketQ(q):
174 return contentsQ(q).pop(0)
175
176
177 def isPacketQ(q):
178 return type(q) == type(()) and ("PQ",[],1) and type(q[1]) == type([])
179
180
181
182
183
184
185 def isEmptPacketQ(q):
186 return contentsQ(q) == []
187
188
189 if __name__ == '__main__':
190 fptr = open(os.environ['OUTPUT_PATH'], 'w')
191
192 first_multiple_input = input().rstrip().split()
193
194 srcIP = str(first_multiple_input[0])
195 dstIP = str(first_multiple_input[1])
196 length = int(first_multiple_input[2])
197 prt = str(first_multiple_input[3])
198 sp = int(first_multiple_input[4])
199 dp = int(first_multiple_input[5])
200 sqn = int(first_multiple_input[6])
201 pld = int(first_multiple_input[7])
202
203 pkt = makePacket(srcIP, dstIP, length, prt, sp, dp, sqn, pld)
204 pk1 = makePacket("111.202.230.44","62.82.29.190",31,"HTTP",80,20,1562431,38)
205 pk2 = makePacket("222.57.155.164","50.168.160.19",22,"UDP",90,5431,1662431,82)
206 pk3 = makePacket("333.230.18.207","213.217.236.184",56,"IRC",501,5643,1762431,318)
207 pk4 = makePacket("444.221.232.94","50.168.160.19",1003,"TCP",4657,4875,1962431,428)
208 pk5 = makePacket("555.221.232.94","50.168.160.19",236,"TCP",7753,5724,2062431,48)
209
210 pkt_list = [pkt,pk1,pk2,pk3,pk4]
211
212 q = makePacketQueue()
213
214 ProtocolList = ["HTTP","SMTP","UDP","TCP","DHCP"]
215 IpBlackList = ["213.217.236.184","444.221.232.94","149.88.83.47","223.70.250.146","169.51.6.136","229.223.169.245"]
216
217 ScoreList = makeScore(pkt_list)
218 addPacket(ScoreList, pk5)
219
220
221 addToPacketQ(pkt,q)
222 addToPacketQ(pk1,q)
223 addToPacketQ(pk2,q)
224 addToPacketQ(pk3,q)
225 addToPacketQ(pk4,q)
226 addToPacketQ(pk5,q)
227 removeFromPacketQ(q)
228
229 fptr.write('Queue Contents => ' + str(contentsQ(q)) + '\n')
230 fptr.write('Is Queue Object => ' + str(isPacketQ(q)) + '\n')
231 fptr.write('Is Queue Object (2) => ' + str(isPacketQ(("PQ",[],1))) + '\n')
232 fptr.write('Is Empty Queue => ' + str(isEmptPacketQ(q)) + '\n')
233 fptr.write('Is Empty Queue (2) => ' + str(isPacketQ(("PQ",[],))) + '\n')
234
235
236
237 fptr.close()
What I have tried:
I dont know what to try. I am stuck - please help and be patient.