A urllib opener can route requests through a proxy and maintain website cookies at the same time. Give it both handlers:

from http.cookiejar import CookieJar
from urllib.request import (
    HTTPCookieProcessor, ProxyHandler, build_opener,
)

cookies = CookieJar()
opener = build_opener(
    ProxyHandler({"http": "http://proxy.example:8080"}),
    HTTPCookieProcessor(cookies),
)
with opener.open("http://www.example.com/", timeout=10) as response:
    body = response.read()

The example uses a plain HTTP destination and its explicit proxy route. For a permitted public-site check without proxy authentication, choose a candidate from the HTTP proxy list for that route while keeping the intended cookie jar. Reuse opener for later requests belonging to the same website session. The cookie processor extracts acceptable response cookies and adds applicable ones to subsequent requests.

Creating a new empty CookieJar before every call discards that continuity. Keep the jar alongside the session it represents; use separate jars for accounts or workflows whose cookies should remain separate.

A basic CookieJar holds state in memory. For persistence between program runs, choose an appropriate FileCookieJar subclass and save or load it deliberately.

There is no need to install a global opener for this arrangement. Calling this object's open method keeps the proxy and cookies attached to the same operation.

Ũhoro ũhĩtĩte