Long-Lived Proxies in Python: Keep Resource Renewal Separate from Job Recovery

By GlobalProxyIP Editorial Team

A long-lived proxy may reduce resource replacement frequency, but it does not make a Python collector continuously available. Plan the resource lifecycle and job recovery separately.

A plan's validity, an individual exit's usable lifetime, the process uptime, and the target's session lifetime describe different clocks. None can be inferred from another merely because a product is sold for a month.

Define what must survive an interruption

An independent read may be repeatable. A target cursor may depend on a session. A partially saved page may need idempotent storage. Before writing retries, identify the unit of work and the point at which it becomes durably complete.

Marking a task complete immediately after dispatch risks omissions. Saving a record without reliable completion tracking can lead to duplicates on restart. Use stable keys and an appropriate storage transaction or recovery design.

Start with a small validated read

The example below expects a controlled endpoint accepting page and returning an items array with nonempty string id values. Use Python 3 and requests==2.34.2, an HTTP username/password proxy, and the nonsecret variables PROXY_HOST, PROXY_PORT, and DATA_URL.

import os, json, time
from pathlib import Path
from getpass import getpass
from urllib.parse import quote
import requests

user, password = input("Proxy username: "), getpass("Proxy password: ") proxy = (f"http://{quote(user, safe='')}:{quote(password, safe='')}" f"@{os.environ['PROXY_HOST']}:{int(os.environ['PROXY_PORT'])}") proxies = {"http": proxy, "https": proxy} seen = set() # Three pages and a two-second pause are teaching values, not target permission. with requests.Session() as session, Path("records.jsonl").open("w", encoding="utf-8") as out: session.trust_env = False for page in range(1, 4): with session.get(os.environ["DATA_URL"], params={"page": page}, proxies=proxies, timeout=(5, 20), allow_redirects=False) as response: if response.status_code in {403, 429}: raise RuntimeError("Stop and review access or rate limits") response.raise_for_status() if not 200 <= response.status_code < 300: raise ValueError("Unexpected redirect") if "application/json" not in response.headers.get("Content-Type", "").lower(): raise ValueError("Expected a JSON response") payload = response.json() if not isinstance(payload, dict) or not isinstance(payload.get("items"), list): raise ValueError("Expected an items array") for item in payload["items"]: if not isinstance(item, dict) or not isinstance(item.get("id"), str) or not item["id"]: raise ValueError("Expected a nonempty string id") if item["id"] not in seen: out.write(json.dumps(item, ensure_ascii=False) + "\n") seen.add(item["id"]) if page < 3: time.sleep(2) print("unique_records", len(seen))

This script demonstrates validation and in-memory deduplication only. It overwrites records.jsonl; run it in a dedicated test directory. It has no durable checkpoint or cross-restart deduplication. The page limit and delay are teaching values, not permission to send requests at that rate.

Environment-based settings are isolated; configure an approved CA path explicitly if needed.

Add renewal at a safe boundary

When a documented resource deadline approaches, stop assigning new units to it. If the resource fails naturally, classify the failure before acquiring another. Bad credentials, an invalid target contract, and an expired resource should not all trigger the same infinite loop.

After replacing a resource, validate whether the target permits continuing the old workflow. A new source plus an old cookie is not automatically a valid continuation. Resume from a documented, authorized recovery point.

Test the recovery design

In a controlled environment, interrupt before a response, after parsing, and after saving but before committing progress. On restart, compare the final record set with a known expected set. A collector that runs again without exceptions may still have omissions.

Keep request attempts, logical tasks, and unique saved records as separate counts. Report recovery only for the conditions actually tested. Longer resource validity is useful when it matches a workflow; reliable job completion still depends on the application design.

Frequently asked questions

Does a monthly plan mean one exit lasts a month?

Not necessarily. Plan validity and individual resource lifetime must be checked separately.

Can the sample resume after a crash?

No. It shows a validation path, not durable recovery. Production needs persistent completion tracking and idempotent storage.

Should every failure trigger a new proxy?

No. Classify authentication, resource, network, target, and data failures before deciding how to recover.

Sources

Sources reviewed on November 20, 2026.