I am writing a Python 2.7 script using Requests to automate access to a website that sets two cookies with the same name, but different domains, E.g. Name 'mycookie', Domain 'www.example.com' and 'subdomain.example.com'. My client-side script needs to read the value of one of these cookies and include it as a parameter in a subsequent request. Because cookie access in a requests.Session
seems to be keyed solely by cookie name, I don't see a way to extract the correct cookie's value. Indeed, an attempt to access the cookies using the name yields this error:
value = session.cookies["mycookie"]
File "/usr/lib/python2.7/site-packages/requests/cookies.py", line 276, in __getitem__
return self._find_no_duplicates(name)
File "/usr/lib/python2.7/site-packages/requests/cookies.py", line 326, in _find_no_duplicates
raise CookieConflictError('There are multiple cookies with name, %r' % (name))
requests.cookies.CookieConflictError: There are multiple cookies with name, 'mycookie'
This suggests that Requests has been written with an assumption that cookie names are unique per session. However this is not necessarily true, as demonstrated.
I think I can work around this by maintaining two sessions and manually copying the other important cookies between them. However I'd like to know if this is a known limitation with Requests and if so what the recommended workaround might be?
Session.cookies
is not a dictionary, it's a RequestsCookieJar
. Try using the method RequestsCookieJar.get()
, which is defined like this:
def get(self, name, default=None, domain=None, path=None):
"""Dict-like get() that also supports optional domain and path args in
order to resolve naming collisions from using one cookie jar over
multiple domains. Caution: operation is O(n), not O(1)."""
try:
return self._find_no_duplicates(name, domain, path)
except KeyError:
return default
For your code, this would mean changing to:
value = session.cookies.get("mycookie", domain=relevant_domain)
As for requests, we know that cookie names aren't unique. =)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With