If session.proxies points to one endpoint but the script uses the shell's proxy, reassigning the same dictionary will not resolve the conflict. Requests can merge environment proxy settings over Session defaults. Choose whether the Session should continue using environment discovery.

Keep environment discovery, supply routes on the request

Pass explicit values to the individual call, as Requests recommends for avoiding this Session/environment conflict:

proxies = {
    "http": "http://127.0.0.1:8080",
    "https": "http://127.0.0.1:8080",
}

with requests.Session() as session:
    response = session.get(
        "https://example.com/",
        proxies=proxies,
        timeout=(5, 15),
    )
    response.raise_for_status()

Replace the local listener with yours. If you are comparing public HTTP endpoints, take candidates from the HTTP proxy list while keeping the Session policy fixed. This controls the supplied mapping entries; make a deliberate choice for any other schemes or hosts the application uses. A service, IDE, container or CI runner can inherit a different environment from your terminal.

Make the Session own its settings

If the application should not discover proxies from its environment, set the policy before the first request:

with requests.Session() as session:
    session.trust_env = False
    session.proxies = {
        "http": "http://127.0.0.1:8080",
        "https": "http://127.0.0.1:8080",
    }
    response = session.get(
        "https://example.com/",
        timeout=(5, 15),
    )
    response.raise_for_status()

trust_env=False also affects environment-related certificate configuration and automatic netrc authentication. Account for those dependencies before choosing it. For an approved private CA bundle, you can pass an explicit path:

response = session.get(
    "https://example.com/",
    timeout=(5, 15),
    verify="/path/to/approved-ca-bundle.pem",
)

Use the real approved bundle and leave verification enabled. Keep a separate Session for each routing policy instead of mutating one while other work uses it.

If neither alternative explains the application's route, compare an isolated Session with the application using the same runtime and harmless destination. Record which proxy variables are present, the trust_env value and whether the call passes proxies; keep credential-bearing URLs out of shared logs. An isolated success directs the next inspection toward Session construction, adapters, prepared-request flows or custom transports. A curl environment test cannot settle Requests' own merge behavior.

متعلقہ مضامین