Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

Python
  1  #!/bin/python3
  2  
  3  import math
  4  import os
  5  import random
  6  import re
  7  import sys
  8  
  9  #
 10  # Please Paste all Fuctions from Part 1,2,3 & 4
 11  # Complete the functions below.
 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.
Posted
Updated 28-Nov-22 0:03am
v13
Comments
Richard MacCutchan 27-Nov-22 4:14am    
I just tried that and the result is:
Is Queue Object (2) => True
Coding Help 27-Nov-22 6:34am    
it should be false
Richard MacCutchan 27-Nov-22 6:53am    
See Solution 2 below.
Coding Help 27-Nov-22 6:38am    
Thanks in advance
Coding Help 27-Nov-22 6:35am    
that is my error it is saying true instead of false

Getting your code to run at all does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
No, it is correctly saying True, as per the code:
Python
def isPacketQ(q):
    return type(q) == type(()) and q[0] == "PQ" and type(q[1]) == type([])

You call isPacketQ passing the tuple ("PQ",[],1). It then tests if the type is a tuple, which is True. If item 0 is "PQ", which is also true. And finally if item 1 is a list type, which again is True. So all three tests being True, that is what it returns.
 
Share this answer
 
Comments
Richard MacCutchan 27-Nov-22 7:22am    
Then you need to change the code. As it stands the test correctly returns True as its result. If that is not correct then you need to rework the test(s) based on whatever rules the application is supposed to be following.
Richard MacCutchan 27-Nov-22 8:57am    
I have no idea, because you have not explained what condition(s) the code is supposed to be testing for.
Richard MacCutchan 27-Nov-22 9:20am    
Sorry, not really, especially in such an unformatted state as posted both here and in your question. I presume that you have complete understanding of what you are trying to do with this code, so you should be able to specify exactly what each function is supposed to be doing.
Coding Help 27-Nov-22 9:13am    
i feel like i am annoying you at this point
Coding Help 27-Nov-22 7:01am    
Is Queue Object => True
Is Queue Object (2) => False
Is Empty Queue => False
Is Empty Queue (2) => True

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900