0% found this document useful (0 votes)
7 views

Foundation Programming

Uploaded by

Raja Fawad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Foundation Programming

Uploaded by

Raja Fawad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CSC5020 Assignment III

Team ID:
Student IDs and Names:

1. Meeting attendance and code


contribution summary
Participants No. of Meeting Code contribution in final No. of Line
Name meeting time version contributed
attend
Student 1 3 10 am -12 line 1-Line 44 (function 118
p.m., 20 host_visited_both_days)
Sep
2 pm -4 Line 1-Line 74(function
pm, 22 Sep generate_abnormal_summary}
Student 2 Line 1-Line 49(function 104
parse_log_file)
9 am – 11
am, 23 sep Line 1-Line 55(function
count_abnormal_visits)

Contribution Summary

Student 1 concentrated on resolving problems 1 and 4, which entailed creating a


summary of unusual visits in the log and taking information from the log file to
demonstrate which host had visited the website on both days. Student 2 oversaw
problem 2, which required parsing each line into fields and using the proper
variables or data structures to store them, and 3, which involved identifying
unsuccessful visiting codes (400 indicates client error, 404 indicates failure) to
count the suspected abnormal client visits. For this assignment, we did three
face-to-face meetings at the university. During meetings, we discuss the solution
of tasks and we both write two functions. So, none of us feels a burden. We both
contributed almost equally.
Problems encountered

We had certain difficulties and hurdles throughout the job that needed a quick
response. Technical difficulties, time restrictions, and comprehension of the
assignment's complicated elements were some of these difficulties. To get past
them:

 To overcome technological obstacles, we performed the study and checked


pertinent material.
 Despite competing schedules, deadlines were met via the use of efficient time
management techniques.
 To comprehend and address complicated challenges, collaboration and
communication were improved more effectively.

Reflection

This task has taught us important lessons and given us insightful new
information:
Better Problem-Solving Skills: We became more confident in our ability to
solve challenging programming issues and successfully implement solutions.
Increased Teamwork: As we delegated jobs and communicated effectively, our
ability to work together increased.
Time management: To reach project milestones, we discovered how important
proactive time management is.
For upcoming programming projects, we advise:
Upkeep of precise Project Plans: Establishing and following precise project
plans with distinct dates.
Regular Communication: Keeping lines of communication open and
transparent to quickly resolve problems.
Continuous Learning: Encouragement of ongoing learning and skill
improvement to successfully complete challenging assignments.

2. Pseudocode

• Write one Python program that reads input from the log file and
shows which host visited the website on both days.
Pseudocode

hosts_Day_1 = empty set


hosts_Day_2 = empty set
Ip_format = regex pattern for IP addresses
Date_format = regex pattern for date
For each line in the log file:
Use regular expression 'Ip_format' to match and extract IP address
Use regular expression 'Date_format' to match and extract date
If ip_match and date_match are successful:
ip = ip_match.group()
date = date_match.group(1)
If date equals Day_1:
Elif date equals Day_2:
Add 'ip' to 'hosts_Day_2' set
hosts_visited_both_days = hosts_Day_1.intersection(hosts_Day_2)
Return hosts_visited_both_days
if __name__ equals "__main__":
path_of_file
Day_1 = '03/Jul/2023'
Day_2 = '04/Jul/2023'
'hosts_visited_both_days' function
hosts_both_days = call 'hosts_visited_both_days' function with
'path_of_file', 'Day_1', and 'Day_2' as arguments
Print "Hosts that visited on", Day_1, "and", Day_2, ":"
For each 'host' in 'hosts_both_days':
Print 'host'

Source code
Output
 Parse each line into fields, using appropriate variables or data
structures to store them. Find the top 10 visiting URL in the log.

Pseudocode

function parse_log_file(log_file_path):
url_stats = an empty dictionary
ip_pattern = a regex pattern for IP addresses
url_pattern = a regex pattern for URLs
For each line in the log file:
Use 'ip_pattern' to search and extract IP address
Use 'url_pattern' to search and extract URL
If ip_match and url_match are successful:
ip = ip_match.group()
url = url_match.group(2)
Increment the 'visits' count for 'url' in 'url_stats'
Add 'ip' to the set of IPs associated with 'url' in 'url_stats'
Return 'url_stats'
if __name__ equals "__main__":
log_file_path
url_statistics = call 'parse_log_file' function with 'log_file_path'
Sort 'url_statistics' by visit counts in descending order
Print "Top 10 Visiting URLs:"
For each 'url' and 'stats' in the sorted URL statistics (first 10 items):
total_visits = 'stats'['visits']
unique_ips = Count the number of elements in 'stats'['ips'] set
Print "URL:", 'url', "| Total Visits:", total_visits, "| Unique IPs:",
unique_ips

Source code
Output
 Counting the suspected abnormal client visiting by detecting
unsuccess visiting codes

Pseudocode

function count_abnormal_visits(log_file_path):
client_error_counts = an empty dictionary
failure_counts = an empty dictionary
ip_pattern = a regex pattern for IP addresses
code_pattern = a regex pattern for visiting codes
Open the log file at 'log_file_path' for reading
For each line in the log file:
Use 'ip_pattern' to search and extract IP address
Use 'code_pattern' to search and extract visiting code

If ip_match and code_match are successful:


ip = ip_match.group()
code = int(code_match.group())
If code equals 400:
Elif code equals 404:
abnormal_visits = {
'Client Errors (400)': client_error_counts,
'Failures (404)': failure_counts
}
Return 'abnormal_visits'
if __name__ equals "__main__":
log_file_path
abnormal_visits = call 'count_abnormal_visits' function with 'log_file_path'
Print "Suspected Abnormal Client Visits:"
For each 'category' and 'counts' in 'abnormal_visits':
Print category + ":"
For each 'ip' and 'count' in 'counts':
Print "IP:", 'ip', "| Count:", 'count'

Source code
Output
 The task involves generating a summary of abnormal visits in the
log, including information such as the fail access times (404 code
indicating a failed visit), successful visiting times (200 code
indicating a successful visit), and the maximum visiting frequency
per hour, determined by checking the frequency of 404 errors.

Pseudocode

function generate_abnormal_summary(log_file_path):
ip_visits = an empty dictionary
ip_pattern = a regex pattern for IP addresses
code_pattern = a regex pattern for visiting codes
timestamp_pattern = a regex pattern for timestamps
Open the log file at 'log_file_path' for reading
For each line in the log file:
Use 'ip_pattern' to search and extract IP address
Use 'code_pattern' to search and extract visiting code
Use 'timestamp_pattern' to search and extract timestamp
If ip_match and code_match and timestamp_match are successful:
ip = ip_match.group()
code = int(code_match.group())
timestamp = timestamp_match.group()
If code equals 200:
Increment the 'Success Count' for 'ip' in 'ip_visits'
Elif code equals 404:
Increment the 'Failure Count' for 'ip' in 'ip_visits'
Append 'timestamp' to the list of 'timestamps' for 'ip' in 'ip_visits'
Increment the 'hourly_frequency' count for 'timestamp' in 'ip_visits[ip]
['hourly_frequency']'
Initialize an empty dictionary 'summary'
For each 'ip' and 'info' in 'ip_visits':
success_count = 'info['Success Count']'
failure_count = 'info['Failure Count']'
timestamps = 'info['timestamps']'
hourly_frequency = Find the maximum count in
'info['hourly_frequency']'
'summary[ip]' = {
'Success Count': success_count,
'Failure Count': failure_count,
'Fail Access Times': failure_count,
'Success Visiting Times': success_count,
'Maximum Visiting Frequency per Hour': hourly_frequency
}
Return 'summary'
if __name__ equals "__main__":
log_file_path = 'log.txt'
abnormal_summary = call 'generate_abnormal_summary' function with
'log_file_path'
For each 'ip' and 'data' in 'abnormal_summary':
Print "IP Address:", 'ip'
Print "Success Count:", 'data['Success Count']'
Print "Failure Count:", 'data['Failure Count']'
Print "Fail Access Times:", 'data['Fail Access Times']'
Print "Success Visiting Times:", 'data['Success Visiting Times']'
Print "Maximum Visiting Frequency per Hour:", 'data['Maximum Visiting
Frequency per Hour']'

Source code
Output

You might also like