A cookie jar is the part of a client that stores the cookies a server sends and attaches them to later requests. The jar lives in your code. A proxy does not store cookies, it only forwards traffic. So when you switch proxies, the jar keeps sending the same cookie from the new IP. A site that ties the session to an IP treats this as suspicious. This post covers where cookies live, how to open a separate jar for each proxy session, how to save the jar to disk and how to pair it with a sticky session.
Where cookies live
A cookie travels between server and client and never piles up in the proxy. The server puts a Set-Cookie header in its response. The client writes that value to the jar and attaches it to later requests for the same domain in a Cookie header.
The proxy is only a carrier in this flow. If the target is http://, the proxy sees the headers but stores nothing. If the target is https://, the client opens a CONNECT tunnel. The proxy cannot read the encrypted traffic inside the tunnel and never sees the cookie.
| Layer | Sees the cookie | Stores it |
|---|---|---|
| The jar in your code | ✓ | ✓ |
Proxy, http:// target | ✓ in the header | ✗ |
Proxy, https:// target (CONNECT) | ✗ | ✗ |
| Target site | ✓ | Keeps its own session record |
In our test, our local proxy logged the Cookie header on http:// requests but kept no state between two requests. The client was always the one adding the cookie to the next request. If you want to recover or reset a session, look at the jar, not the proxy settings.
One jar per session
The right setup fits in one sentence: one account, one proxy session, one jar. The jar lives inside the client object. It starts empty when you open the object and leaves memory when you close it.
| Library | Jar | Writing to disk |
|---|---|---|
| Requests | Session.cookies | Assign an LWPCookieJar, then save() |
| HTTPX | httpx.Client(cookies=...) | save() on the same LWPCookieJar |
Node, undici fetch | None | tough-cookie CookieJar and serialize() |
Requests and HTTPX accept the jars from Python's http.cookiejar module. In our test, the LWPCookieJar we passed to httpx.Client became the client's own jar: client.cookies.jar is jar returned True. Node's built-in fetch, on the other hand, stores no cookies. You set up the jar yourself with an open-source package such as tough-cookie.
This setup is only for your own account or for access the account owner has approved. A jar carries a login session. Carrying someone else's session breaks the site's terms of use and the law.
The cookie trap in rotation
A jar picks cookies by domain and ignores the IP. If you change the proxy on the same Session object, the login cookie goes out from the new IP on the next request. You get no error. The server, meanwhile, sees the same session at two different addresses.
In our test, our local site bound the session to the first IP it saw. We logged in through proxy A, then switched session.proxies to proxy B in the same session. The site returned 401: the session had been opened at 127.0.0.2 and the request came from 127.0.0.3. Using a jar restored from disk with a different proxy gave the same result.
Real sites react differently. Some end the session, some ask for extra verification, some do nothing. Since you cannot know which one will happen, set the rule on the client side:
- Bind each jar to a single Sticky Proxy session and leave the proxy setting unchanged for the whole session.
- If the IP changes, discard the jar and log in again with a new session.
- Do not give parallel workers a shared jar. Each worker gets its own session and its own jar.
We explained how sticky duration and the session ID work in What Is a Sticky Session?. On Proxynet you choose a duration between 1 and 60 minutes.
Saving the jar to disk
Saving to disk lets a script pick the session up where it left off after a restart. In Python, LWPCookieJar writes the jar to a readable text file. The trap is a single parameter: by default, save() drops session cookies.
A session cookie is a cookie without Expires or Max-Age. Under RFC 6265, the client keeps it only "until the current session is over". Login cookies are often of this type. In our test, a file saved without ignore_discard held only the persistent lang cookie, not the sid login cookie.
The module below keeps a separate file for each session. If the file has sat untouched for longer than the sticky duration, it does not load it and deletes it instead. After a gap that long, the exit IP has certainly changed.
import time
from http.cookiejar import LWPCookieJar
from pathlib import Path
import requests
JAR_DIR = Path("jars")
STICKY_SECONDS = 30 * 60 # match the sticky duration you chose
def open_session(name, proxy_url):
"""Open a Session that carries its own cookie jar for one sticky session."""
JAR_DIR.mkdir(exist_ok=True)
path = JAR_DIR / f"{name}.lwp"
jar = LWPCookieJar(path)
if path.exists():
idle = time.time() - path.stat().st_mtime
if idle < STICKY_SECONDS:
jar.load(ignore_discard=True) # load session cookies too
else:
path.unlink() # the IP has long changed, do not carry the old cookie
session = requests.Session()
session.cookies = jar
session.proxies = {"http": proxy_url, "https": proxy_url}
return session
def close_session(session):
session.cookies.save(ignore_discard=True) # otherwise session cookies are not written
session.close()
session = open_session("account-1", "http://user:pass@pr.proxynet.io:8000")
try:
session.get("https://panel.example.com/report", timeout=20)
finally:
close_session(session)The check works one way only. Even if the file is fresh, the exit device may have dropped off the network. If the site sends you to the login screen, delete the jar and log in again. In HTTPX you pass the same jar with httpx.Client(proxy=proxy_url, cookies=jar). The save and load lines stay the same.
The login form, the CSRF token and detecting a dropped session are covered in Sessions and Cookies in Python. This post deals only with how the jar relates to the proxy session.
tough-cookie in Node
In Node, the tough-cookie package holds the jar and undici's fetch sends the request. The code joining the two has three steps: read the Cookie header from the jar before the request, write each Set-Cookie line to the jar after the response, and save the jar as JSON when you are done.
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { CookieJar } from "tough-cookie";
import { fetch, ProxyAgent } from "undici";
const PROXY = "http://user:pass@pr.proxynet.io:8000";
const JAR_FILE = "jars/account-1.json";
const agent = new ProxyAgent(PROXY);
async function loadJar() {
try {
const data = JSON.parse(await readFile(JAR_FILE, "utf8"));
return await CookieJar.deserialize(data);
} catch {
return new CookieJar(); // empty jar if there is no file
}
}
async function saveJar(jar) {
await mkdir("jars", { recursive: true });
await writeFile(JAR_FILE, JSON.stringify(await jar.serialize()));
}
async function get(jar, url) {
const cookie = await jar.getCookieString(url);
const res = await fetch(url, {
dispatcher: agent,
headers: cookie ? { cookie } : {},
redirect: "manual", // keep the Set-Cookie from the intermediate response
});
for (const line of res.headers.getSetCookie()) {
await jar.setCookie(line, url);
}
return res;
}
const jar = await loadJar();
await get(jar, "https://panel.example.com/report");
await saveJar(jar);Save the file with the .mjs extension. There are two differences from Python. tough-cookie's serialize() output already includes session cookies, so no extra parameter is needed. The second difference is in redirects. By default, fetch follows a 302 on its own, and the Set-Cookie in the intermediate response never reaches you. In our test, an endpoint that set the login cookie with a 302 ended in 401 under the default setting. With redirect: "manual", the cookie was written to the jar. With this setting you follow the Location header yourself.
Debugging
Cookie problems are usually silent. The code throws no error, the site just does not recognize you. Check in this order, by symptom:
- The login screen appears after a restart. Open the file. If there is no login cookie such as
sid,ignore_discard=Trueis missing. - The session drops midway. Log the exit IP at each step. If the IP changed, the jar has moved to a different address.
- The jar is empty after login in Node. The login response is a redirect. Use
redirect: "manual". - Two workers break each other's sessions. They share a jar. Give each worker its own client object.
- The cookie is not sent. Check the domain and path in the jar. A cookie marked
Secureis not sent to anhttp://address.
How we tested
We ran the tests on December 1, 2026, on a local setup: Python 3.13.17, Requests 2.34.2, HTTPX 0.28.1, Node 24.22.2, undici 8.10.0 and tough-cookie 6.0.2. The target was a small local site that bound the session cookie to the first IP it saw. Two local proxies sat in front of it. One exited from 127.0.0.2, the other from 127.0.0.3, and both only forwarded and logged without keeping state. We ran the save and restore steps in separate processes.
The bottom line
Give each account one proxy session and one jar, and write the jar to disk together with its session cookies. Above all: when the IP changes, do not carry the jar to the new address, discard it. If you need to keep a session on the same address for days, use ISP Proxy.
Sources
- RFC 6265, HTTP State Management Mechanism, section 4.1.2.2 Max-Age: rfc-editor.org
- RFC 9110, HTTP Semantics, section 9.3.6 CONNECT: rfc-editor.org
- http.cookiejar, FileCookieJar.save: docs.python.org
- tough-cookie, RFC 6265 Cookies and CookieJar for Node.js: github.com
Frequently asked questions
Does a proxy store cookies?
No. The jar in the client stores the cookie and adds it to later requests. The proxy only forwards traffic. With an http:// target it can see the headers; with an https:// target it never sees the cookie because of the encrypted tunnel. Cookies are not lost when the proxy changes, they keep going out from the same jar with the new IP. To reset a session, clear the jar.
Can I use a logged-in session with rotating proxies?
It is not recommended. With rotation, every new connection gets a different IP, while the jar keeps sending the same login cookie. A site that ties sessions to an IP may read this as a sign of a hijacked session. For work that requires a login, use a sticky session, bind the jar to it and log in again if the IP changes.
Why was the session cookie not written to the file?
You did not pass ignore_discard=True. By default, Python's FileCookieJar.save() skips cookies marked for discard, meaning session cookies without Expires or Max-Age. Sites' login cookies are often of this type. Add ignore_discard=True to both the save() and load() calls. tough-cookie's serialize(), on the other hand, already includes these cookies.
Can I use one Session for multiple accounts?
No. The cookies of two accounts on the same domain overwrite each other in the same jar. They also all go out with the same proxy setting. Open a separate requests.Session or httpx.Client for each account and give each one its own proxy session and its own cookie file. This rule applies only to accounts you own or are authorized to use.
Can I use saved cookies the next day?
You can, but in a sticky session the IP will have changed by then. The next day the same cookie goes out from a different address, and the site may ask you to log in again. If you need the same address for a long time, use a static IP. In a sticky session, do not keep the jar longer than the session duration; log in again when it expires.
Where should I store the cookie file?
Somewhere private. A login cookie stands in for the password for that session. Do not add the file to version control; put it in .gitignore. Keep it out of shared folders and backups. On Linux and macOS, set the file permissions so only you can read it. Delete the file when you are done or when you log out of the account.




