← All notes

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

Sources

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

Permanent ID: iil:note:sgnrrmap

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.},
}

Markdown

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.

All artifact files (JSON index)

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.