Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert requests.RequestsCookieJar to string

I have a requests.RequestCookieJar object which contains multiple cookies from different domain/path. How can I extract a cookies string for a particular domain/path following the rules mentioned in here?

For example

>>> r = requests.get("https://stackoverflow.com")
>>> print(r.cookies)
<RequestsCookieJar[<Cookie prov=4df137f9-848e-01c3-f01b-35ec61022540 for .stackoverflow.com/>]>

# the function I expect
>>> getCookies(r.cookies, "stackoverflow.com")
"prov=4df137f9-848e-01c3-f01b-35ec61022540"

>>> getCookies(r.cookies, "meta.stackoverflow.com")
"prov=4df137f9-848e-01c3-f01b-35ec61022540"
# meta.stackoverflow.com is also satisfied as it is subdomain of .stackoverflow.com

>>> getCookies(r.cookies, "google.com")
""
# r.cookies does not contains any cookie for google.com, so it return empty string
like image 853
hgminh Avatar asked Apr 12 '18 02:04

hgminh


People also ask

What is CookieJar in Python?

cookiejar module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data – cookies – to be set on the client machine by an HTTP response from a web server, and then returned to the server in later HTTP requests.

What does CookieJar do?

CookieJar is an object for storing cookies that are set by the server, added by the client, or both. As described in the how-to guide on using Cookies, k6 handles cookies automatically by default.

How do I send a cookie request in Python?

To send a request with a Cookie, you need to add the "Cookie: name=value" header to your request. To send multiple cookies in a single Cookie header, separate them with semicolons or add multiple "Cookie: name=value" request headers.


3 Answers

I think you need to work with a Python dictionary of the cookies. (See my comment above.)

def getCookies(cookie_jar, domain):
    cookie_dict = cookie_jar.get_dict(domain=domain)
    found = ['%s=%s' % (name, value) for (name, value) in cookie_dict.items()]
    return ';'.join(found)

Your example:

>>> r = requests.get("https://stackoverflow.com")
>>> getCookies(r.cookies, ".stackoverflow.com")
"prov=4df137f9-848e-01c3-f01b-35ec61022540"
like image 108
nofinator Avatar answered Oct 22 '22 20:10

nofinator


NEW ANSWER

Ok, so I still don't get exactly what it is you are trying to achieve.

If you want to extract the originating url from a requests.RequestCookieJar object (so that you could then check if there is a match with a given subdomain) that is (as far as I know) impossible.

However, you could off course do something like:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import requests
import re

class getCookies():

    def __init__(self, url):

        self.cookiejar = requests.get(url).cookies
        self.url = url

    def check_domain(self, domain):

        try:

            base_domain = re.compile("(?<=\.).+\..+$").search(domain).group()

        except AttributeError:

            base_domain = domain

        if base_domain in self.url:

            print("\"prov=" + str(dict(self.cookiejar)["prov"]) + "\"")

        else:

            print("No cookies for " + domain + " in this jar!")

Then if you do:

new_instance = getCookies("https://stackoverflow.com")

You could then do:

new_instance.check_domain("meta.stackoverflow.com")

Which would give the output:

"prov=5d4fda78-d042-2ee9-9a85-f507df184094"

While:

new_instance.check_domain("google.com")

Would output:

"No cookies for google.com in this jar!"

Then, if you (if needed) fine-tune the regex & create a list of urls, you could first loop through the list to create many instances and save them in eg a list or dict. In a second loop you could check another list of urls to see if their cookies might be present in any of the instances.


OLD ANSWER

The docs you link to explain:

items()

Dict-like items() that returns a list of name-value tuples from the jar. Allows client-code to call dict(RequestsCookieJar) and get a vanilla python dict of key value pairs.

I think what you are looking for is:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import requests

def getCookies(url):

    r = requests.get(url)

    print("\"prov=" + str(dict(r.cookies)["prov"]) + "\"")

Now I can run it like this:

>>> getCookies("https://stackoverflow.com")
"prov=f7712c78-b489-ee5f-5e8f-93c85ca06475"
like image 33
Montmons Avatar answered Oct 22 '22 21:10

Montmons


actually , when I just have the problem as you are. but when I access the Class Define

class RequestsCookieJar(cookielib.CookieJar, MutableMapping):

I found a func called def get_dict(self, domain=None, path=None): you can simply write code like this

raw = "rawCookide"
print(len(cookie))
mycookie = SimpleCookie()
mycookie.load(raw)
UCookie={}
for key, morsel in mycookie.items():
    UCookie[key] = morsel.value
like image 23
Oasis Avatar answered Oct 22 '22 20:10

Oasis