ITP 270 Programming Assignment 2 Spring 2021
65 Points
I found a file of downloaded traffic from a firewall and after filtering it to make it more manageable for our
class (it had over 67000 records to begin with), I have 716 records that you can analyze by reading the file into
either several parallel lists (each with 1 column) or 1 two dimensional list and then finding descriptive statistics
of traffic for some well known destination ports and the largest and smallest packet size in terms of bytes. The
input requires you to read a textfile which contains 716 records of firewall traffic (FirewallFileSpring2022.txt)
that can be downloaded from Canvas. (This is a file input, list processing, descriptive statistics, and exception
handling problem). To help you solve this problem with the least amount of aggravation, let’s divide it into 6
Logical Tasks.
This version of the assignment should be completed only by students whose first names begin with A, B, C
Task 1: Read the file and create the list(s), then print the list contents, and the number of rows (size) in the
list.
Within your python script, I suggest that you use either Python’s csv library or Python’s IO library to open the
FirewallFileSpring2022.txt txt file. But, before you do that, take a look at the FirewallFileSpring2022.txt by
opening it into Excel or any text editor, but do not change its contents. You will see 5 columns but no
headings. Column 1 shows the source port. Column 2 shows the destination port. Column 3 shows the action
(deny or drop). Column 4 shows the size in bytes. Column 5 shows the number of packets. The file has a
delimiter which is a tab (‘\t’). Each row in the file must be read into separate lists or a two dimensional list
whichever you prefer. (Using Python’s csv library, it is easiest to read into a two dimensional list.) After you
have read the rows into the list, you should print the list to see how it looks. To help you to see if you have
read in the file, below is a shortened version of the firewall list which is a two-dimensional list (there are 716
rows but below are the first 5 lines of output and I have indicated where I am not showing all of the lines of
output up until the point of the last 5 lines of output:
runfile('/Users/sherrivaseashta/Documents/Spring2022/ITP270/firewall_solution.py',
wdir='/Users/sherrivaseashta/Documents/Spring2022/ITP270')
[['0', '0', 'deny', '98', '1'], ['0', '0', 'deny', '98', '1'], ['0', '0', 'deny', '98',
'1'], ['0', '0', 'deny', '98', '1'], ['0', '0', 'deny', '82', '1'], ['0', '0', 'deny',
'98', '1'], ['0', '0', 'deny', '98', '1'], ['0', '0', 'deny', '60', '1'], ['0', '0',
'deny', '98', '1'], ['0', '0', 'deny', '60', '1'], ['0', '0', 'deny', '60', '1'], ['0',
'0', 'deny', '94', '1'], ['0', '0', 'deny', '70', '1'], ['0', '0', 'deny', '156', '2'],
['0', '0', 'deny', '156', '2'], ['0', '0', 'deny', '156', '2'], ['0', '0', 'deny', '156',
'2'], ['0', '0', 'deny', '102', '1'], ['0', '0', 'deny', '70', '1'], ['0', '0', 'deny',
'70', '1'], ['0', '0', 'deny', '98', '1'], ['0', '0', 'deny', '98', '1'], ['0',
.....there are lots and lots of lines of output not shown here.......'1'], ['6666', '80', 'deny', '60', '1'], ['60000', '80', 'deny', '60', '1'], ['59059',
'80', 'deny', '62', '1'], ['11643', '81', 'deny', '60', '1'], ['35268', '81', 'deny',
'60', '1'], ['34485', '81', 'deny', '60', '1'], ['25469', '81', 'deny', '60', '1'],
['43332', '81', 'deny', '60', '1'], ['64649', '81', 'deny', '60', '1'], ['20168', '81',
'deny', '60', '1'], ['63814', '81', 'deny', '60', '1'], ['10354', '81', 'deny', '60',
'1'], ['12221', '81', 'deny', '60', '1'], ['47030', '81', 'deny', '60', '1'], ['15274',
'81', 'deny', '60', '1'], ['9659', '81', 'deny', '60', '1'], ['58022', '88', 'deny',
'60', '1']]
To be sure that you’ve read all of records correctly into the list(s), print a label as shown below and the
number of rows in the list using the list function/method that tells you how many rows are in a list. If you have
read all of the rows correctly and are using the correct function, you should see the following:Number of rows in the list: 716
Task 2: Convert strings to integers for columns that contain numbers that you will be analyzing.
You will need to use a for loop to process each row wherein you change the datatype for the following
columns to an integer.
The first column within the text file has source port data that should be converted to an integer.
The second column within the text file has destination port data that should be converted to an integer.
The third column within the text file has action taken (deny) and is already a string so no conversion is
necessary.
The fourth column within the text file has size in bytes data that should be converted to an integer.
The fifth column within the text file has number of packets data that should be converted to an integer.
After the loop ends, for the first row, check the data types for each column by using the Python function that
shows you the data type. As shown below using a 2 dimensional list with rows and columns, print a label and
the data types for each column in the first row:data type of row 0, col 0 -->
data type of row 0, col 1 -->
data type of row 0, col 2 -->
data type of row 0, col 3 -->
data type of row 0, col 4 -->
If you are using parallel lists, then you may want to alter the above to include the name of the list followed by
row 0 since a parallel list is a single dimensional list with only 1 column but many rows:data type of list1, row 0 -->
data type of list2, row 0 -->
data type of list3, row 0 -->
data type of list4, row 0 -->
data type of list5, row 0 -->
Notice that columns 0, 1, 3, 4 are now int whereas column 2 is still a string since it contains data like drop or
deny. If you can’t change the necessary columns to integers, then you may not be able to finish the
assignment.
Task 3: For packets with source port 68 BOOTP Client to destination port 67 BOOTP Server, count and
display those packets, and find the most bytes in a packet and least bytes in a packet and display the count,
the packet with the most bytes and packet with the least bytes. Here are detailed instructions
Print headings that sayTask 3
Port 68 BOOTP Client to Port 67 BOOTP Server
Packet Count, Packet w/Most Bytes, Packet w/Least Bytes
Use a for loop to process all rows within the list(s). Within the for list, include an if statement that finds
packets with source port of 68 and destination port of 67. Within this if statement, for the very first packet
found, set the size for the least bytes and most bytes equal to whatever the bytes value is for the row that the
loop is at. Print what you find for each to make sure that they show 346, and break out of the loop. If you do
this correctly, you should see the following: Most Bytes 346
Least Bytes 346Then, assuming that you see that the Most Bytes is set to 346 and Least Bytes is set to 346, comment the print
lines out. The purpose of the loop and if statement is merely to find the bytes at the first record that meets
the condition of Source Port 68 to Destination Port 67.
After the for loop ends, print the headings as shown below:Row # Source Dest. Action Bytes Packets
Then, start another for loop (preferably using Python’s range function) to process all rows within the list(s).
Within the for list, include an if statement that finds packets with source port of 68 and destination port of 67.
Set up a counter to count the number of packets with source port of 68 and destination port of 67. Print each
row and when printing the row, notice that you are supposed to print a row # in the first column. Assuming
the for loop started with a value of 0, you should be able to add 1 to the loop counter to display the row
number shown for each record meeting the condition. Within this if statement, use another if statement to
determine if the bytes for the current row are lower than the Least Bytes set previously. If yes, set a new value
for Least Bytes to be equal to the bytes for the current row. Use another if statement to determine if the
bytes for the current row are higher than the Most Bytes set previously. If yes , set a new value for Most Bytes
to be equal to the bytes for the current row. Then after the for loop ends, print the updated values for Count,
the Most Bytes and Least Bytes. All of the display that you should see for Task 3 is shown below:
Task 3
Port 68 BOOTP Client to Port 67 BOOTP Server
Packet Count, Packet w/Most Bytes, Packet w/Least Bytes
Row # Source Dest Action Bytes Packets
671 68 67 deny 346 1
674 68 67 deny 594 1
675 68 67 deny 346 1
676 68 67 deny 594 1
677 68 67 deny 346 1
678 68 67 deny 346 1
679 68 67 deny 346 1
686 68 67 deny 594 1
691 68 67 deny 594 1
68 BOOTP Client to 67 BOOTP Server Count 9
68 BOOTP Client to 67 BOOTP Server Most Bytes 594
68 BOOTP Client to 67 BOOTP Server Least Bytes 346
Task 4: Display and count the number of records for Port 81 TorPark Onion Routing and the Percent of Port
81 TorPark Onion Routing from all packets in the file:
Print the headers as shown:
Destination Port 81 TorPark Onion Routing: Count and Percent of All Packets
Row # Source Dest Action Bytes Packets
Use a for loop to process the list. Within the for loop, use an if statement to identify all packets with
Destination (shown as Dest below) port of 81 which is TorPark Onion Routing. Within the if statement, be sure
to count the number of packets and display each row for packets with Destination port of 81. After the for
loop ends, calculate display the count and % that Port 81 TorPark Onion Routing packets make of all packets
in the list. Here are the results that you should see if you code this correctly.Destination Port 81 TOR Count and Percent of All Packets
Row Source Dest Action Bytes Packets
703 11643 81 deny 60 1
704 35268 81 deny 60 1
705 34485 81 deny 60 1
706 25469 81 deny 60 1
707 43332 81 deny 60 1
708 64649 81 deny 60 1
709 20168 81 deny 60 1
710 63814 81 deny 60 1
711 10354 81 deny 60 1
712 12221 81 deny 60 1
713 47030 81 deny 60 1
714 15274 81 deny 60 1
715 9659 81 deny 60 1
Port 81 TorPark Onion Routing Count 13
Port 81 TorPark Onion Routing Percent 1.8
NOTE for all students: Print or String Formatting is required to display the table results in columns that are
aligned and decimal places should appear as they do in results shown.
Task 5: Create a output file (FirewallStatistics.txt) to save the statistics from Step 4 (according to your last
name) into the output file.
Most Bytes: 594
Least Bytes: 346
Port 81 TOR Count: 13
Port 81 TOR Percent: 1.8156424581005588
Task 6: Use some exception handling to let you know if the files could not be loaded, if a list index error
occurred, and if any other error occurred. For any other error, capture the type of error thrown by the system
and display it.
HINT: ProgrammingLab4 and ProgrammingLab5 cover lists and file processing with exception handling.
(ProgrammingLab1 – ProgrammingLab3 cover decision structures and loops and string and print formatting.
Please refer to the Extra Credit for Programming Assignment 2 video that you can watch that will have a
similar problem.)