cookies (sashahart)

2.2.1 · abandoned · verified Thu Apr 16

The `cookies` library (by Sasha Hart) is a Python module designed for parsing and rendering RFC 6265-compliant HTTP `Cookie:` request headers and `Set-Cookie:` response headers. It offers a cleaner API compared to the standard library's `http.cookies` and explicitly adheres to modern cookie standards, eschewing backward compatibility with older, less compliant RFCs (like 2109 or 2965). The last release was in 2014 (v2.2.1), and there is no apparent active development, suggesting it is no longer maintained.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating, manipulating, and rendering 'Set-Cookie' headers, and parsing 'Cookie' request headers.

from cookies import Cookies, Cookie

# Create a Cookies object for response headers
response_cookies = Cookies()
response_cookies['session_id'] = 'abc123def456'
response_cookies['session_id'].path = '/'
response_cookies['session_id'].max_age = 3600 # 1 hour

# You can also set using a Cookie object directly
my_cookie = Cookie('user', 'guest')
my_cookie.domain = 'example.com'
response_cookies.add(my_cookie)

print('Set-Cookie Headers:')
for header_line in response_cookies.render_response():
    print(header_line)

# Simulate receiving a Cookie header from a request
request_header_string = 'session_id=abc123def456; other_data=some_value'
request_cookies = Cookies.from_request(request_header_string)

print('\nParsed Request Cookies:')
for name, cookie in request_cookies.items():
    print(f"  {name}: {cookie.value}")

# Access a specific cookie
if 'session_id' in request_cookies:
    print(f"Session ID from request: {request_cookies['session_id'].value}")

view raw JSON →