Python http cookies: parse and write HTTP cookies
Answer: The cookies module defines classes for abstracting the concept of cookies, an HTTP state management mechanism.
Classes
BaseCookie is a dictionary-like object whose keys are strings.
The values stored there are Morsel instances.
When you set a key, the value is first converted to a Morsel.
SimpleCookie derives from BaseCookie and supports strings as cookie values.
Parsing and errors
On encountering an invalid cookie, CookieError is raised.
If cookie data comes from a browser, you should catch CookieError on parsing.
Version 3.3 allowed a colon as a valid cookie name character.
The cookies module and the cookiejar module do not depend on each other.
See also
Claims
- id: C1 claim: The module defines classes that abstract HTTP cookies. source: S1 evidence: "defines classes for abstracting the concept of cookies"
- id: C2 claim: SimpleCookie supports strings as cookie values. source: S1 evidence: "SimpleCookie supports strings as cookie values"
- id: C3 claim: BaseCookie is a dictionary-like object whose keys are strings. source: S2 evidence: "This class is a dictionary-like object whose keys are strings"
- id: C4 claim: The cookies and cookiejar modules do not depend on each other. source: S2 evidence: "do not depend on each other"
Sources
- [S1] http.cookies — HTTP state management. Python Software Foundation. https://docs.python.org/3/library/http.cookies.html . License: PSF License Version 2. Accessed 2026-11-24.
- [S2] http.cookies documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/http.cookies.rst . License: PSF License Version 2. Accessed 2026-11-24.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, http.cookies — HTTP state management; CPython, http.cookies documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, http.cookies — HTTP state management; CPython, http.cookies documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python http cookies: parse and write HTTP cookies." iinventedlight Library, iil:note:sgnrrmap, version of 2026-11-24. https://iinventedlight.com/library/notes/python-http-cookies/. License: CC-BY-4.0.
@misc{iil_note_sgnrrmap,
author = {{iinventedlight}},
title = {{Python http cookies: parse and write HTTP cookies}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-24},
url = {https://iinventedlight.com/library/notes/python-http-cookies/},
note = {Permanent ID iil:note:sgnrrmap. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-http-cookies.schema.json (schema; CC-BY-4.0)
- python-http-cookies.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-http-cookies.md.