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 questions
Answer options
A: browser.cookies._cooktes['.gpyc.com/MUID'] .expires
B: browser.cookies._cookies['.gpyc.com'] ['/'] ['MUID'].expires Correct answer
C: browser.cookies._cookies.gpyc.com.MUID.expires
D: browser.cookies.gpyc.com['MUID']. expires
E: browser.cookies.gpyc.com.MUID.expires
Correct Answer:
B. browser.cookies._cookies['.gpyc.com'] ['/'] ['MUID'].expires
Explanation

The 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.

Why Incorrect

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. ---

References

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

Scroll to Top