nerdexam
Cisco

200-901 · Question #678

Drag and Drop Question Drag and drop the code from the bottom onto the box where the code is missing to print the hostname, software version, and uptime for the network devices returned in the…

The correct answer is PrettyTable; content-type; response; network_devices. This question tests the ability to complete a Python script that uses the requests library to consume a REST API and the prettytable library to display the retrieved data in a formatted table.

Infrastructure and Automation

Question

Drag and Drop Question Drag and drop the code from the bottom onto the box where the code is missing to print the hostname, software version, and uptime for the network devices returned in the response. Not options are used. Answer:

Exhibit

200-901 question #678 exhibit

Answer Area

Drag items

network_devicesdevicesPrettyTablerequestresponseencodingcontent-type

Correct arrangement

  • PrettyTable
  • content-type
  • response
  • network_devices

Explanation

This question tests the ability to complete a Python script that uses the requests library to consume a REST API and the prettytable library to display the retrieved data in a formatted table.

Approach. To correctly complete the Python script, each blank must be filled with the appropriate drag target based on Python syntax and library usage:

  1. Blank 1 (network_devices = ): The variable network_devices is later used with the .add_row() method, which is a feature of PrettyTable objects. Therefore, network_devices must be initialized as an instance of PrettyTable. The correct drag target is PrettyTable.
  2. Blank 2 (ctp = { : 'application/json' }): The ctp dictionary is passed as headers to the requests.get() function. 'application/json' is a common value for the Content-Type HTTP header, specifying the expected format of the response. The correct drag target is content-type.
  3. Blank 3 (data = .json()): After a requests.get() call, the returned object is a requests.Response object. To parse its JSON content into a Python dictionary or list, the .json() method is called on this response object. The correct drag target is response.
  4. Blank 4 ( .add_row([...])): Inside the for loop, data from the API response is added as a row to the table. The table object initialized in Blank 1 is network_devices. Thus, network_devices.add_row() is the correct way to add a row to the table. The correct drag target is network_devices.

Common mistakes.

  • common_mistake. Common mistakes include incorrectly identifying the object for method calls or using generic terms instead of specific library objects or header keys.
  • Using devices: 'devices' is not a defined object in this context and cannot be used to initialize a PrettyTable or call .add_row().
  • Using request: 'request' refers to the requests module itself, not the response object that contains the data to be parsed using .json().
  • Using encoding: While related to data format, encoding is not the correct HTTP header key to specify the content type for JSON; Content-Type is used for this purpose.

Concept tested. This question tests fundamental Python programming concepts, including variable assignment, dictionary creation, function definition, for loops, and- crucially- the practical application of external libraries: the requests library for consuming REST APIs (making GET requests, handling headers, and parsing JSON responses) and the prettytable library for formatting and displaying tabular data.

Topics

#network automation#data parsing#API interaction#Python scripting

Community Discussion

No community discussion yet for this question.

Full 200-901 Practice