Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requests - inability to handle two cookies with same name, different domain

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?

like image 875
davidA Avatar asked Feb 12 '23 07:02

davidA


1 Answers

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

like image 166
Lukasa Avatar answered Feb 13 '23 21:02

Lukasa