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

2024/4/15 1:06:46

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?

Answer

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 inorder to resolve naming collisions from using one cookie jar overmultiple 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. =)

https://en.xdnf.cn/q/73094.html

Related Q&A

Python logging from multiple processes

I have a possibly long running program that currently has 4 processes, but could be configured to have more. I have researched logging from multiple processes using pythons logging and am using the So…

Error while fetching Tweets with Tweepy

I have a Python script that fetch tweets. In the script i use the libary Tweepy . I use a valid authentication parameters. After running this script some tweets are stored in my MongoDB and some are r…

Example to throw a BufferError

On reading in the Python 3.3 documentation I noticed the entry about a BufferError exception: "Raised when a buffer related operation cannot be performed.". Now Im wondering in which cases co…

Which algorithm would fit best to solve a word-search game like Boggle with Python

Im coding a game similar to Boggle where the gamer should find words inside a big string made of random letters.For example, there are five arrays with strings inside like this. Five rows, made of six …

Sort using argsort in python

I try to sort an array: import numpy as nparr = [5,3,7,2,6,34,46,344,545,32,5,22] print "unsorted" print arrnp.argsort(arr)print "sorted" print arrBut the output is:unsorted [5, 3, …

pandas DataFrame resample from irregular timeseries index

I want to resample a DataFrame to every five seconds, where the time stamps of the original data are irregular. Apologies if this looks like a duplicate question, but I have issues with the interpolati…

django makemigrations to rename field without user input

I have a model with CharField named oldName. I want to rename the field to newName. When I run python manage.py makemigrations, I get a confirmation request "Did you rename model.oldName to model.…

Global Python packages in Sublime Text plugin development

1. SummaryI dont find, how Sublime Text plugins developer can use Sublime Text find global Python packages, not Python packages of Sublime Text directory.Sublime Text use own Python environment, not Py…

Can I use pip install to install a module for another users?

Im wish to install Numpy for the www-data user, but I can not log into this user using login. How can I make www-data make us of the Numpy module?To clarify. Numpy is available for root, and for my de…

Set dynamic node shape in network with matplotlib

First time poster here, so please be gentle. :)Im trying to graph a network of characters of different types in Networkx and want to set different node shapes for each type. For example, Id like chara…