nerdexam
Cisco

200-901 · Question #548

Refer to the exhibit. A developer created a Python script by using the Cisco Meraki API. The solution must: - Obtain a list of HTTP servers for a network named "net1". - Print the response body if…

The correct answer is A. except requests.Timeout as e: print("Timeout Error: {}". Format(e)) if response.status_code ==200: print(response.text). Properly catching a requests timeout requires the module-prefixed exception class, capturing the instance with 'as', and interpolating the variable - not a string literal - into the printed message.

Understanding and Using APIs

Question

Refer to the exhibit. A developer created a Python script by using the Cisco Meraki API. The solution must:

  • Obtain a list of HTTP servers for a network named "net1".
  • Print the response body if the HTTP status code is 200.
  • Handle the timeout requests as exceptions, and print Timeout Error

next to the exception to stdout. Which block of code completes the script? A. B. C. D.

Exhibit

200-901 question #548 exhibit

Options

  • Aexcept requests.Timeout as e: print("Timeout Error: {}". Format(e)) if response.status_code ==200: print(response.text)
  • Bexcept Timeout as exception: print("Timeout Error") if status_code == 200: print(response.text)
  • Cexcept requests Timeout: print("Timeout Error") if response.status_code == 200: print(response)
  • Dexcept requests.Timeout as exception: print("Timeout Error: exception") if response.status_code == 200: print(response)

How the community answered

(50 responses)
  • A
    84% (42)
  • B
    10% (5)
  • C
    2% (1)
  • D
    4% (2)

Why each option

Properly catching a requests timeout requires the module-prefixed exception class, capturing the instance with 'as', and interpolating the variable - not a string literal - into the printed message.

Aexcept requests.Timeout as e: print("Timeout Error: {}". Format(e)) if response.status_code ==200: print(response.text)Correct

except requests.Timeout as e correctly references the Timeout exception from the requests module and binds the exception instance to e. The call print("Timeout Error: {}".format(e)) interpolates the actual exception object so its details appear next to the label, satisfying the requirement to print Timeout Error next to the exception. The subsequent check for response.status_code == 200 and print(response.text) correctly prints the response body when the request succeeds.

Bexcept Timeout as exception: print("Timeout Error") if status_code == 200: print(response.text)

except Timeout as exception omits the required requests. module prefix, which would raise a NameError at runtime, and if status_code == 200 references an undefined variable instead of response.status_code.

Cexcept requests Timeout: print("Timeout Error") if response.status_code == 200: print(response)

except requests Timeout is a syntax error because it is missing the dot separator between requests and Timeout, and the block prints the raw response object instead of response.text.

Dexcept requests.Timeout as exception: print("Timeout Error: exception") if response.status_code == 200: print(response)

print("Timeout Error: exception") passes the string literal 'exception' rather than the captured exception variable, so the actual exception details are never included in the output.

Concept tested: Python requests module exception handling and response body output

Source: https://requests.readthedocs.io/en/latest/user/quickstart/#errors-and-exceptions

Topics

#Python programming#Cisco Meraki API#API error handling#REST API interaction

Community Discussion

No community discussion yet for this question.

Full 200-901 Practice