Skip to content Skip to sidebar Skip to footer

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

Solution 1:

Session.cookies is not a dictionary, it's a RequestsCookieJar. Try using the method RequestsCookieJar.get(), which is defined like this:

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

Post a Comment for "Requests - Inability To Handle Two Cookies With Same Name, Different Domain"