For a header defined by the proxy operator, Requests exposes HTTPAdapter.proxy_headers as an override point:

from urllib.parse import urlsplit
import requests
from requests.adapters import HTTPAdapter

class TaggedProxy(HTTPAdapter):
    def proxy_headers(self, proxy):
        headers = super().proxy_headers(proxy)
        endpoint = urlsplit(proxy)
        if (endpoint.scheme == 'http'
                and endpoint.hostname == 'proxy.example'
                and endpoint.port == 8080):
            headers['X-Proxy-Account'] = 'department-a'
        return headers

with requests.Session() as session:
    adapter = TaggedProxy()
    session.mount('http://', adapter)
    session.mount('https://', adapter)
    response = session.get(
        'https://service.example/data',
        proxies={'https': 'http://proxy.example:8080'},
        timeout=15,
    )

X-Proxy-Account is an example of a field understood by this particular proxy, not a standard way to authenticate to every intermediary.

Start with super().proxy_headers(proxy). Requests uses that method to generate Proxy-Authorization when the URL supplies a Basic account. Replacing the whole result can discard that behavior.

The adapter passes these values into urllib3's ProxyManager. For an HTTPS tunnel, they accompany CONNECT. The ordinary request headers mapping describes the later destination request.

Proxy managers are cached by proxy URL. Do not treat this hook as an uncached per-business-request callback for a value that must change on every call. Choose transport ownership accordingly when an account marker varies.

For plain HTTP forwarding, proxy fields still need the intermediary's intended consumption policy. A header being proxy-specific in the client API is not a confidentiality guarantee if the proxy forwards it onward.

Tënë sô zo