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

  1. Get request cookies:
    Use cookie.get_cookie or cookie.get_cookies to get all
  2. Set cookies:
    Create a new cookie.Cookie object, optionally add attributes to it, then use cookie.set_cookie to make it be sent with the HTTP response.
  3. Get response cookies:
    Use cookie.get_response_cookies.

Reference

class krait.cookie.Cookie(name, value, attributes=None)[source]

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.
name

str – The name of the cookie.

value

str – The value of the cookie.

attributes

list of CookieAttribute – A (possibly empty) list of attributes. Update only with instance methods.

add_attribute(attribute)[source]

Add an attribute to the cookie.

:param attribute CookieAttribute: The attribute to add.

remove_attribute(name)[source]

Remove an attribute from the cookie.

Parameters:name (str) – The name of the attribute to remove (e.g. Expires).
set_expires(expires_datetime)[source]

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_max_age(max_age)[source]

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_path(path)[source]

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_domain(domain)[source]

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_secure(is_secure)[source]

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_http_only(is_http_only)[source]

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.
class krait.cookie.CookieAttribute(name, value)[source]

Bases: object

A generic cookie attribute.

Parameters:
  • name (str) – The name of the attribute.
  • value (str, optional) – The value of the attribute.
name

str – The name of the attribute.

value

str – The value of the attribute.

class krait.cookie.CookieExpiresAttribute(expire_datetime)[source]

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 at
expire_datetime

datetime.datetime – the moment that the cookie expires at

class krait.cookie.CookieMaxAgeAttribute(max_age)[source]

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.
max_age

int – The lifetime of the cookie, in seconds.

class krait.cookie.CookiePathAttribute(path)[source]

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.
path

str – The URL to which to restrict the cookie.

class krait.cookie.CookieDomainAttribute(domain)[source]

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.
domain

str – The domain on which the cookie is restricted.

class krait.cookie.CookieHttpOnlyAttribute[source]

Bases: krait.cookie.CookieAttribute

Sets the HttpOnly attribute on the cookie. This causes the cookie to be inaccessible from Javascript.

class krait.cookie.CookieSecureAttribute[source]

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).

krait.cookie.get_cookies()[source]

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.

krait.cookie.get_response_cookies()[source]

Get cookies already set with cookie.setCookie() or direct header manipulation

Returns:the response cookies already set.
Return type:list of cookie.Cookie

Set a new (or updated) cooke.

Parameters:cookie (cookie.Cookie) – the cookie item.