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_duplicatesraise 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?