The office proxy asks for one account. The website behind it asks for another. Requests has separate inputs for those two Basic logins: credentials in the proxy URL and the request's auth argument.

from urllib.parse import quote
import requests

proxy_url = (
    f"http://{quote(proxy_user, safe='')}:"
    f"{quote(proxy_password, safe='')}@proxy.example:8080"
)
response = requests.get(
    "https://reports.example/private/",
    proxies={"https": proxy_url},
    auth=(site_user, site_password),
    timeout=(5, 20),
)
response.raise_for_status()

The four account variables come from the application's credential configuration. Encoding the proxy username and password separately preserves characters such as an at sign without turning them into URL delimiters.

The https dictionary key selects this route for an HTTPS destination. Its value still starts with http:// because that is the protocol used to reach this example proxy.

For that HTTPS request, the proxy login belongs to the CONNECT exchange. The website login belongs to the later request through the established tunnel. Letting the adapter derive the proxy authorization from its own URL keeps it at the relevant hop; no combined authentication class is needed for this Basic case.

A 407 identifies the intermediary's challenge. A 401 from the website identifies the destination's challenge. Use that response to decide which of the two configured accounts needs attention.

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