# Http.cookiejar: support file-like objects for FileCookieJar and its sub-classes

**URL:** <https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458>\
**Category:** Ideas\
**Created:** [October 13, 2026, 6:02am UTC](https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458 "2026-10-13T06:02:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hiccup](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@Hiccup](https://discuss.python.org/u/Hiccup)\
**Post date:** [October 13, 2026, 6:02am UTC](https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458/1 "2026-10-13T06:02:20Z")

</div>

Currently, [http.cookiejar](https://docs.python.org/3/library/http.cookiejar.html) only supports path-like objects for FileCookieJar and its sub-classes (MozillaCookieJar and LWPCookieJar). So there’s no clean way to load interoperable cookie format data other than from the disk.

Reasoning:

- Cookie files can contain sensitive info, so you may not want to write it to disk.
- Its useful to provide the user other options to load data, other than from the disk, especially with keys/“secrets” (which cookies can contain). E.g. environment variable or standard input.
- As I understand it, its preferable for python libraries to support file-like objects rather than just paths, when the path itself is not relevant (as in this case - http.cookiejar only uses for error messages for failing to load the path).

Workarounds:

- Using the private function `_really_load` (not desirable as its a private function)
- Making a fork of http.cookiejar (not desirable as it requires maintenance and this appears to require a small change to the http.cookiejar code)

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 13, 2026, 7:10am UTC](https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458/2 "2026-10-13T07:10:34Z")

</div>

What would it mean to provide a file-like object? Would that simply load the cookies but not save? None of the rest of your post refers to being able to mutate the cookie jar, but I’m not certain if that’s your intention here.

An unsaveable cookie jar subclass that can be given an existing file’s contents would seem like a useful feature. But if it needs to be able to save back afterwards, that’d be more complicated.

---

<div class="post-metadata">

**Author:** ![Hiccup](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@Hiccup](https://discuss.python.org/u/Hiccup)\
**Post date:** [October 13, 2026, 8:31am UTC](https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458/3 "2026-10-13T08:31:33Z")

</div>

> [@Rosuav](#):
>
> What would it mean to provide a file-like object?

You can read from and write to the same [file-like object](https://docs.python.org/3/library/io.html) by adding the `+` mode: [Built-in functions — Python 3.15.0 documentation](https://docs.python.org/3/builtins/functions.html#open)

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 13, 2026, 8:46am UTC](https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458/4 "2026-10-13T08:46:02Z")

</div>

> [@Hiccup](#):
>
> You can read from and write to the same [file-like object](https://docs.python.org/3/library/io.html) by adding the `+` mode [Built-in functions — Python 3.15.0 documentation](https://docs.python.org/3/builtins/functions.html#open)

That’s talking about an actual `file` object, not the much larger concept of a “file-like” object. For a few very obvious examples of distinctions, a file object has a `.fileno()` method that gives its file descriptor, but in general, file-like objects won’t (because the entire _point_ is that they don’t have to be backed by file descriptors); and they won’t be expected to support file locking, memory mapping, or anything of that nature. They also aren’t necessarily seekable.

> **[io — Core tools for working with streams](https://docs.python.org/3/library/io.html)**
>
> Source code: Lib/io.py Overview: The io module provides Python’s main facilities for dealing with various types of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These ar...

The `io` module, and particularly `io.StringIO` and `io.BytesIO`, will give you a good idea of what people usually expect of a file-like object. It is not uncommon for file-like objects to be EITHER readable OR writable, but not both; one such example is what you get from a zipfile:

> **[zipfile — Work with ZIP archives](https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.open)**
>
> Open a ZIP file, where file can be a path to a file (a string), a file-like object or a path-like object. | Source code: Lib/zipfile/ The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write,...

When you have an open zip file, you can open a file inside that, and get back a readable or writable file-like object, but there’s no mention of `+` being valid here. TBH this would potentially be a good use-case for a read-only cookiejar - you could have an archive of all your cookies, encrypted for a bit of extra protection, and then you perform HTTP requests on them directly.

---

<div class="post-metadata">

**Author:** ![Hiccup](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@Hiccup](https://discuss.python.org/u/Hiccup)\
**Post date:** [October 13, 2026, 9:11am UTC](https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458/5 "2026-10-13T09:11:58Z")

</div>

I see.

For my specific use case, I’d think I would need io.StringIO, so I could provide a string from an environment variable. I don’t need to be able to write to the file. (Although for my use case, it could probably just be a plain string, rather than io.StringIO, I guess).

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 13, 2026, 9:14am UTC](https://discuss.python.org/t/http-cookiejar-support-file-like-objects-for-filecookiejar-and-its-sub-classes/109458/6 "2026-10-13T09:14:11Z")

</div>

> [@Hiccup](#):
>
> For my specific use case, I’d think I would need io.StringIO, so I could provide a string from an environment variable. I don’t need to be able to write to the file. (Although for my use case, it could probably just be a plain string, rather than io.StringIO, I guess).

Ah, that might be an alternate and equally valid approach. (I don’t know enough about the `http` module to know how its cookie jar is implemented, so I don’t know which would be easier.) A cookie jar that can be directly loaded from a (byte?) string would be quite useful.

You’re likely a lot more familiar with the code than I am; perhaps you could write a patch, adding either StringIO/BytesIO or plain str/bytes loading. Then someone smarter than me can evaluate whether it’s worth adding.
