CookieJar#
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#
Methods#
- class CookieJar
- add_cookie(cookie: Cookie) → None#
Adds
cookietojar.Emits the
changedsignal 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).cookiewill be ‘stolen’ by the jar, so don’t free it afterwards.- Parameters:
cookie – a
Cookie
- add_cookie_full(cookie: Cookie, uri: Uri | None = None, first_party: Uri | None = None) → None#
Adds
cookietojar.Emits the
changedsignal 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_partywill be used to reject cookies coming from third party resources in case such a security policy is set in thejar.uriwill be used to reject setting or overwriting secure cookies from insecure origins.Noneis treated as secure.cookiewill be ‘stolen’ by the jar, so don’t free it afterwards.- Parameters:
cookie – a
Cookieuri – the URI setting the cookie
first_party – the URI for the main document
- add_cookie_with_first_party(first_party: Uri, cookie: Cookie) → None#
Adds
cookietojar.Emits the
changedsignal 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_partywill be used to reject cookies coming from third party resources in case such a security policy is set in thejar.cookiewill 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
Listwith every cookie inside thejar.The cookies in the list are a copy of the original, so you have to free them when you are done with them.
For historical reasons this list is in reverse order.
- delete_cookie(cookie: Cookie) → None#
Deletes
cookiefromjar.Emits the
changedsignal.- Parameters:
cookie – a
Cookie
- get_accept_policy() → CookieJarAcceptPolicy#
Gets
jar'sCookieJarAcceptPolicy.
- get_cookie_list(uri: Uri, for_http: bool) → list[Cookie]#
Retrieves the list of cookies that would be sent with a request to
urias aListofCookieobjects.If
for_httpisTrue, 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). SinceCookieJarsets the Cookie header itself when making the actual HTTP request, you should almost certainly be settingfor_httptoFalseif you are calling this.- Parameters:
uri – a
Urifor_http – whether or not the return value is being passed directly to an HTTP operation
- get_cookie_list_with_same_site_info(uri: Uri, top_level: Uri | None, site_for_cookies: Uri | None, for_http: bool, is_safe_method: bool, is_top_level_navigation: bool) → list[Cookie]#
This is an extended version of
get_cookie_listthat provides more information required to use SameSite cookies.See the SameSite cookies spec for more detailed information.
- Parameters:
uri – a
Uritop_level – a
Urifor the top level documentsite_for_cookies – a
Uriindicating the origin to get cookies forfor_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_httpisFalseis_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_httpisTrue, 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). SinceCookieJarsets the Cookie header itself when making the actual HTTP request, you should almost certainly be settingfor_httptoFalseif you are calling this.- Parameters:
uri – a
Urifor_http – whether or not the return value is being passed directly to an HTTP operation
- set_accept_policy(policy: CookieJarAcceptPolicy) → None#
Sets
policyas the cookie acceptance policy forjar.- Parameters:
policy – a
CookieJarAcceptPolicy
- set_cookie(uri: Uri, cookie: str) → None#
Adds
cookietojar, exactly as though it had appeared in a Set-Cookie header returned from a request touri.Keep in mind that if the
CookieJarAcceptPolicyset is eitherNO_THIRD_PARTYorGRANDFATHERED_THIRD_PARTYyou’ll need to useset_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
- set_cookie_with_first_party(uri: Uri, first_party: Uri, cookie: str) → None#
Adds
cookietojar, exactly as though it had appeared in a Set-Cookie header returned from a request touri.first_partywill be used to reject cookies coming from third party resources in case such a security policy is set in thejar.- 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.
Signals#
Virtual Methods#
Fields#
- class CookieJar
- parent_instance#