Sobes.tech
Junior — Middle+

Counting the number of different vulnerabilities for each device

livecode

Task condition

It is necessary to refine the current script so that it outputs the number of unique vulnerabilities, grouped by each device_id.


def process_paginated_response(content, res):
    total = 0
    for item in content:
        device_id = item["device_id"]
        vuln_num = len(item["vulnerabilities"])
        res[device_id] += vuln_num
        total += vuln_num
    return total

res = defaultdict(int)
total_ = 0
page_number = 1

while True:
    url = f"{base_url}?page_number={page_number}"
    content_ = requests.get(url, auth=(username, password)).json()
    total_ += process_paginated_response(content_["results"], res)
    if content_["more"] is False:
        break
    page_number += 1

print(json.dumps(dict(res), indent=4))
print(total_)