Skip to content

rnet.cookie

Cookie management and storage for maintaining session state.

rnet.cookie

HTTP Cookie Management

This module provides classes for creating, managing, and storing HTTP cookies in a thread-safe manner. It includes support for all standard cookie attributes and provides a cookie jar for automatic cookie handling during HTTP requests.

SameSite

Bases: Enum

The Cookie SameSite attribute.

Source code in rnet/cookie.py
@final
class SameSite(Enum):
    r"""
    The Cookie SameSite attribute.
    """

    Strict = auto()
    Lax = auto()
    Empty = auto()

Cookie

A cookie.

Source code in rnet/cookie.py
class Cookie:
    r"""
    A cookie.
    """

    name: str
    r"""
    The name of the cookie.
    """
    value: str
    r"""
    The value of the cookie.
    """
    http_only: bool
    r"""
    Returns true if the 'HttpOnly' directive is enabled.
    """
    secure: bool
    r"""
    Returns true if the 'Secure' directive is enabled.
    """
    same_site_lax: bool
    r"""
    Returns true if  'SameSite' directive is 'Lax'.
    """
    same_site_strict: bool
    r"""
    Returns true if  'SameSite' directive is 'Strict'.
    """
    path: str | None
    r"""
    Returns the path directive of the cookie, if set.
    """
    domain: str | None
    r"""
    Returns the domain directive of the cookie, if set.
    """
    max_age: datetime.timedelta | None
    r"""
    Get the Max-Age information.
    """
    expires: datetime.datetime | None
    r"""
    The cookie expiration time.
    """

    def __init__(
        self,
        name: str,
        value: str,
        domain: str | None = None,
        path: str | None = None,
        max_age: datetime.timedelta | None = None,
        expires: datetime.datetime | None = None,
        http_only: bool | None = None,
        secure: bool | None = None,
        same_site: SameSite | None = None,
    ) -> None:
        r"""
        Create a new cookie.
        """
        ...

    def __str__(self) -> str: ...

name instance-attribute

name

The name of the cookie.

value instance-attribute

value

The value of the cookie.

http_only instance-attribute

http_only

Returns true if the 'HttpOnly' directive is enabled.

secure instance-attribute

secure

Returns true if the 'Secure' directive is enabled.

same_site_lax instance-attribute

same_site_lax

Returns true if 'SameSite' directive is 'Lax'.

same_site_strict instance-attribute

same_site_strict

Returns true if 'SameSite' directive is 'Strict'.

path instance-attribute

path

Returns the path directive of the cookie, if set.

domain instance-attribute

domain

Returns the domain directive of the cookie, if set.

max_age instance-attribute

max_age

Get the Max-Age information.

expires instance-attribute

expires

The cookie expiration time.

__init__

__init__(name, value, domain=None, path=None, max_age=None, expires=None, http_only=None, secure=None, same_site=None)

Create a new cookie.

Source code in rnet/cookie.py
def __init__(
    self,
    name: str,
    value: str,
    domain: str | None = None,
    path: str | None = None,
    max_age: datetime.timedelta | None = None,
    expires: datetime.datetime | None = None,
    http_only: bool | None = None,
    secure: bool | None = None,
    same_site: SameSite | None = None,
) -> None:
    r"""
    Create a new cookie.
    """
    ...

Jar

A thread-safe cookie jar for storing and managing HTTP cookies.

This cookie jar can be safely shared across multiple threads and is used to automatically handle cookies during HTTP requests and responses.

By default, cookie compression is enabled to reduce storage overhead. Use uncompressed() to create a variant without compression if needed.

Source code in rnet/cookie.py
class Jar:
    r"""
    A thread-safe cookie jar for storing and managing HTTP cookies.

    This cookie jar can be safely shared across multiple threads and is used
    to automatically handle cookies during HTTP requests and responses.

    By default, cookie compression is enabled to reduce storage overhead.
    Use `uncompressed()` to create a variant without compression if needed.
    """

    def __init__(self, compression: bool | None = None) -> None:
        r"""
        Create a new cookie jar with compression enabled by default.
        """
        ...

    def compressed(self) -> "Jar":
        r"""
        Clone this Jar, sharing storage but enabling compression.
        """
        ...

    def uncompressed(self) -> "Jar":
        r"""
        Clone this Jar, sharing storage but disabling compression.
        """
        ...

    def get(self, name: str, url: str) -> Cookie | None:
        r"""
        Get a cookie by name and URL.
        """
        ...

    def get_all(self) -> Sequence[Cookie]:
        r"""
        Get all cookies.
        """
        ...

    def add(self, cookie: Cookie | str, url: str) -> None:
        r"""
        Add a cookie or cookie string to this jar.
        """
        ...

    def remove(self, name: str, url: str) -> None:
        r"""
        Remove a cookie from this jar by name and URL.
        """
        ...

    def clear(self) -> None:
        r"""
        Clear all cookies in this jar.
        """
        ...

__init__

__init__(compression=None)

Create a new cookie jar with compression enabled by default.

Source code in rnet/cookie.py
def __init__(self, compression: bool | None = None) -> None:
    r"""
    Create a new cookie jar with compression enabled by default.
    """
    ...

compressed

compressed()

Clone this Jar, sharing storage but enabling compression.

Source code in rnet/cookie.py
def compressed(self) -> "Jar":
    r"""
    Clone this Jar, sharing storage but enabling compression.
    """
    ...

uncompressed

uncompressed()

Clone this Jar, sharing storage but disabling compression.

Source code in rnet/cookie.py
def uncompressed(self) -> "Jar":
    r"""
    Clone this Jar, sharing storage but disabling compression.
    """
    ...

get

get(name, url)

Get a cookie by name and URL.

Source code in rnet/cookie.py
def get(self, name: str, url: str) -> Cookie | None:
    r"""
    Get a cookie by name and URL.
    """
    ...

get_all

get_all()

Get all cookies.

Source code in rnet/cookie.py
def get_all(self) -> Sequence[Cookie]:
    r"""
    Get all cookies.
    """
    ...

add

add(cookie, url)

Add a cookie or cookie string to this jar.

Source code in rnet/cookie.py
def add(self, cookie: Cookie | str, url: str) -> None:
    r"""
    Add a cookie or cookie string to this jar.
    """
    ...

remove

remove(name, url)

Remove a cookie from this jar by name and URL.

Source code in rnet/cookie.py
def remove(self, name: str, url: str) -> None:
    r"""
    Remove a cookie from this jar by name and URL.
    """
    ...

clear

clear()

Clear all cookies in this jar.

Source code in rnet/cookie.py
def clear(self) -> None:
    r"""
    Clear all cookies in this jar.
    """
    ...