CookieJar#

class CookieJar(**properties: Any)#

Superclasses: Object

Subclasses: CookieJarDB, CookieJarText

Implemented Interfaces: SessionFeature

Automatic cookie handling for SoupSession.

A CookieJar stores Cookies and arrange for them to be sent with the appropriate Messages. CookieJar implements SessionFeature, so you can add a cookie jar to a session with add_feature or add_feature_by_type.

Note that the base CookieJar class does not support any form of long-term cookie persistence.

Constructors#

class CookieJar
classmethod new() CookieJar#

Creates a new CookieJar.

The base CookieJar class does not support persistent storage of cookies; use a subclass for that.

Methods#

class CookieJar

Adds cookie to jar.

Emits the changed signal if we are modifying an existing cookie or adding a valid new cookie (‘valid’ means that the cookie’s expire date is not in the past).

cookie will be ‘stolen’ by the jar, so don’t free it afterwards.

Parameters:

cookie – a Cookie

Adds cookie to jar.

Emits the changed signal if we are modifying an existing cookie or adding a valid new cookie (‘valid’ means that the cookie’s expire date is not in the past).

first_party will be used to reject cookies coming from third party resources in case such a security policy is set in the jar.

uri will be used to reject setting or overwriting secure cookies from insecure origins. None is treated as secure.

cookie will be ‘stolen’ by the jar, so don’t free it afterwards.

Parameters:
  • cookie – a Cookie

  • uri – the URI setting the cookie

  • first_party – the URI for the main document

Adds cookie to jar.

Emits the changed signal if we are modifying an existing cookie or adding a valid new cookie (‘valid’ means that the cookie’s expire date is not in the past).

first_party will be used to reject cookies coming from third party resources in case such a security policy is set in the jar.

cookie will be ‘stolen’ by the jar, so don’t free it afterwards.

For secure cookies to work properly you may want to use add_cookie_full.

Parameters:
  • first_party – the URI for the main document

  • cookie – a Cookie

all_cookies() list[Cookie]#

Constructs a List with every cookie inside the jar.

The cookies in the list are a copy of the original, so you have to free them when you are done with them.

Deletes cookie from jar.

Emits the changed signal.

Parameters:

cookie – a Cookie

do_changed(self, old_cookie: Cookie, new_cookie: Cookie) None#
Parameters:
  • old_cookie

  • new_cookie

do_is_persistent(self) bool#
do_save(self) None#
get_accept_policy() CookieJarAcceptPolicy#

Gets jar's CookieJarAcceptPolicy.

Retrieves the list of cookies that would be sent with a request to uri as a List of Cookie objects.

If for_http is True, the return value will include cookies marked “HttpOnly” (that is, cookies that the server wishes to keep hidden from client-side scripting operations such as the JavaScript document.cookies property). Since CookieJar sets the Cookie header itself when making the actual HTTP request, you should almost certainly be setting for_http to False if you are calling this.

Parameters:
  • uri – a Uri

  • for_http – whether or not the return value is being passed directly to an HTTP operation

This is an extended version of get_cookie_list that provides more information required to use SameSite cookies.

See the SameSite cookies spec for more detailed information.

Parameters:
  • uri – a Uri

  • top_level – a Uri for the top level document

  • site_for_cookies – a Uri indicating the origin to get cookies for

  • for_http – whether or not the return value is being passed directly to an HTTP operation

  • is_safe_method – if the HTTP method is safe, as defined by RFC 7231, ignored when for_http is False

  • is_top_level_navigation – whether or not the HTTP request is part of top level navigation

get_cookies(uri: Uri, for_http: bool) str | None#

Retrieves (in Cookie-header form) the list of cookies that would be sent with a request to uri.

If for_http is True, the return value will include cookies marked “HttpOnly” (that is, cookies that the server wishes to keep hidden from client-side scripting operations such as the JavaScript document.cookies property). Since CookieJar sets the Cookie header itself when making the actual HTTP request, you should almost certainly be setting for_http to False if you are calling this.

Parameters:
  • uri – a Uri

  • for_http – whether or not the return value is being passed directly to an HTTP operation

is_persistent() bool#

Gets whether jar stores cookies persistenly.

set_accept_policy(policy: CookieJarAcceptPolicy) None#

Sets policy as the cookie acceptance policy for jar.

Parameters:

policy – a CookieJarAcceptPolicy

Adds cookie to jar, exactly as though it had appeared in a Set-Cookie header returned from a request to uri.

Keep in mind that if the CookieJarAcceptPolicy set is either NO_THIRD_PARTY or GRANDFATHERED_THIRD_PARTY you’ll need to use set_cookie_with_first_party, otherwise the jar will have no way of knowing if the cookie is being set by a third party or not.

Parameters:
  • uri – the URI setting the cookie

  • cookie – the stringified cookie to set

Adds cookie to jar, exactly as though it had appeared in a Set-Cookie header returned from a request to uri.

first_party will be used to reject cookies coming from third party resources in case such a security policy is set in the jar.

Parameters:
  • uri – the URI setting the cookie

  • first_party – the URI for the main document

  • cookie – the stringified cookie to set

Properties#

class CookieJar
props.accept_policy: CookieJarAcceptPolicy#

The type of the None singleton.

props.read_only: bool#

The type of the None singleton.

Signals#

class CookieJar.signals
changed(old_cookie: Cookie, new_cookie: Cookie) None#

The type of the None singleton.

Parameters:
  • old_cookie – the old Cookie value

  • new_cookie – the new Cookie value

Virtual Methods#

class CookieJar
do_changed(old_cookie: Cookie, new_cookie: Cookie) None#

The type of the None singleton.

Parameters:
  • old_cookie

  • new_cookie

do_is_persistent() bool#

Gets whether jar stores cookies persistenly.

do_save() None#

The type of the None singleton.

Fields#

class CookieJar
parent_instance#