krait.cookie module¶
This module is used to wrap the usage of cookies in websites. This prevents the need to manipulate the HTTP headers manually.
Usage¶
- Get request cookies:
- Use
cookie.get_cookie
orcookie.get_cookies
to get all
- Set cookies:
- Create a new
cookie.Cookie
object, optionally add attributes to it, then usecookie.set_cookie
to make it be sent with the HTTP response.
- Get response cookies:
- Use
cookie.get_response_cookies
.
Reference¶
Represents an HTTP cookie.
Parameters: - name (str) – The name of the cookie
- value (str) – The value of the cookie.
- attributes (list of
CookieAttribute
, optional) – A (possibly empty) list of attributes. Can be updated later.
str – The name of the cookie.
str – The value of the cookie.
list of
CookieAttribute
– A (possibly empty) list of attributes. Update only with instance methods.
Add an attribute to the cookie.
:param attribute
CookieAttribute
: The attribute to add.
Remove an attribute from the cookie.
Parameters: name (str) – The name of the attribute to remove (e.g. Expires
).
Set or remove the Expires attribute on the cookie. This makes the cookie delete itself after a certain time.
Parameters: expires_datetime ( datetime.datetime
, optional) – The UTC/timezoned time of expiration, or None to remove.
Set or remove the Max-Age attribute on the cookie. This makes the cookie delete itself after a certain number of seconds.
Warning
This attribute is not supported by all browsers. Notably, Internet Explorer does not respect it.
Parameters: max_age (int, optional) – The maximum time that a cookie can be kept, in seconds, or None to remove.
Set or remove the Path attribute on the cookie. This restricts the cookie only to one URL and its descendants.
Parameters: path (str, optional) – The path, or None to remove.
Set or remove the Domain attribute on the cookie. This restricts the domain on which the cookie can be sent by the client.
Parameters: domain (str, optional) – The domain, or None to remove.
Set or remove the Secure attribute on the cookie. This causes the cookie to only be sent over HTTPS (not yet supported by Krait).
Parameters: is_secure (bool) – True to set the attribute, False to remove it.
Set or remove the HTTPOnly attribute on the cookie. This causes the cookie to be inaccessible from Javascript.
Parameters: is_http_only (bool) – True to set the attribute, False to remove it.
Bases:
object
A generic cookie attribute.
Parameters: - name (str) – The name of the attribute.
- value (str, optional) – The value of the attribute.
str – The name of the attribute.
str – The value of the attribute.
Bases:
krait.cookie.CookieAttribute
Sets the Expires attribute on the cookie. This makes the cookie delete itself after a certain time.
Parameters: expire_datetime ( datetime.datetime
) – the moment that the cookie expires atdatetime.datetime
– the moment that the cookie expires at
Bases:
krait.cookie.CookieAttribute
Sets the Max-Age attribute on the cookie. This makes the cookie delete itself after a certain number of seconds.
Warning
This attribute is not supported by all browsers. Notably, Internet Explorer does not respect it.
Parameters: max_age (int) – The lifetime of the cookie, in seconds. int – The lifetime of the cookie, in seconds.
Bases:
krait.cookie.CookieAttribute
Sets the Path attribute on the cookie. This restricts the cookie only to one URL and its descendants.
Parameters: path (str) – The URL to which to restrict the cookie. str – The URL to which to restrict the cookie.
Bases:
krait.cookie.CookieAttribute
Sets the Domain attribute on the cookie. This restricts the domain on which the cookie can be sent by the client.
Parameters: domain (str) – The domain on which the cookie is restricted. str – The domain on which the cookie is restricted.
Bases:
krait.cookie.CookieAttribute
Sets the HttpOnly attribute on the cookie. This causes the cookie to be inaccessible from Javascript.
Bases:
krait.cookie.CookieAttribute
Sets the Secure attribute on the cookie. This causes the cookie to only be sent over HTTPS (not yet supported by Krait).
Get all the cookies sent by the client.
Returns: list of Cookie
Get the value of a single cookie, by name.
Parameters: - name (str) – The name of the cookie to be returned.
- default (str, optional) – The value to be used if the cookie cannot be found.
Returns: The value of the cookie, or the second argument, if it doesn’t exist.
Get cookies already set with
cookie.setCookie()
or direct header manipulationReturns: the response cookies already set. Return type: list of cookie.Cookie
Set a new (or updated) cooke.
Parameters: cookie ( cookie.Cookie
) – the cookie item.