GPYC practice question 3 of 15
With a requests session object named "browser", how can the expiration attribute of a cookie named "MUID" from "gpyc.com/" be accessed?
Full set: 75 questionsThe requests library's session object stores cookies in a RequestsCookieJar object, which is a subclass of the standard library's http.cookiejar.CookieJar. This object uses a private dictionary, cookies, to manage the cookie objects. The dictionary is structured hierarchically: the first level of keys are the domains (e.g., '.gpyc.com'), the second level are the paths (e.g., '/'), and the third level are the cookie names (e.g., 'MUID'). Therefore, to access the specific Cookie object and its expires attribute, one must traverse this nested dictionary structure using key-based indexing.
A. browser.cookies.cookies['.gpyc.com/MUID'] .expires This is incorrect because it attempts to use a single, combined key '.gpyc.com/MUID', which does not match the nested [domain][path][name] dictionary structure.
C. browser.cookies.cookies.gpyc.com.MUID.expires This is incorrect because it uses attribute-style access (.) on a dictionary (cookies), which is not a valid way to access dictionary keys containing special characters like dots.
D. browser.cookies.gpyc.com['MUID']. expires This is incorrect as it tries to access the domain as a direct attribute of the RequestsCookieJar object, which is not how the cookie data is stored or exposed.
E. browser.cookies.gpyc.com.MUID.expires This is incorrect because it improperly uses attribute-style access on the RequestsCookieJar object to retrieve the cookie, which is not a supported method. ---
1. Python Standard Library Source Code (http.cookiejar): The requests library builds upon the standard http.cookiejar module. The internal storage mechanism is defined in the CookieJar class's setcookie method, which uses the line self.cookies.setdefault(domain, {}).setdefault(path, {})[name] = cookie. This code explicitly demonstrates the nested dictionary structure of cookies[domain][path][name].
Source: CPython source code repository, Lib/http/cookiejar.py.
Link: https://github.com/python/cpython/blob/3.11/Lib/http/cookiejar.py#L1389
2. Requests Library Source Code (requests.cookies): The source code for the requests library shows that RequestsCookieJar inherits directly from the standard library's CookieJar (aliased as cookielib for compatibility) and does not alter this internal storage structure.
Source: requests library source code repository, requests/cookies.py.
Reference: The class definition class RequestsCookieJar(cookielib.CookieJar): confirms the inheritance.
Link: https://github.com/psf/requests/blob/main/requests/cookies.py#L135
Keep practicing
Take the free GPYC practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other GPYC practice questions
- Question 1What will the contents of the "__name__" variable be if the program "gpyc.py" is run as a…
- Question 4What does the attacker do in a SQL Injection attack?
- Question 6What will be the value of the i variable when the while loop finishes its execution0 i = …
- Question 8An operator able to perform bitwise shifts is coded as (select two answers)
- Question 9Third-party Python modules like Scapy and Beautiful Soup are used for what purpose?
- Question 13After calling a subprocess in a Python program, the program returns an error code of "0".…
