Send Only One Host Through an HTTPX Proxy
Mount a proxy transport for a selected host while keeping the client's normal fallback route direct.
Give the selected host a proxy transport and the rest of the client an explicit direct transport:
import httpx
direct = httpx.HTTPTransport(trust_env=False)
proxied = httpx.HTTPTransport(
proxy="http://203.0.113.10:8080",
trust_env=False,
)
with httpx.Client(
transport=direct,
mounts={"all://example.com": proxied},
trust_env=False,
timeout=15,
) as client:
response = client.get("https://example.com/")
print(response.status_code)
other = client.get("https://example.org/")
print(other.status_code)
Replace the documentation proxy address with the intended HTTP endpoint from your service or the HTTP proxy list. Here example.com uses the mount and example.org uses the direct default. Both calls matter: success on the proxied host alone cannot verify the fallback route.
| Match detail | Effect |
|---|---|
all://example.com | The exact host across destination schemes. |
| A subdomain | A separate choice; use a supported wildcard deliberately if it should match too. |
| A path | Mounts select scheme, host and optional port patterns, not URL paths. |
| A redirect to another host | When redirect following is enabled, the new URL is matched and may use the direct fallback. |
The example disables environment discovery on the client and both transports. Another broad proxy rule or matching mount can change the result when adapting it. If the deployment relies on environment-provided CA configuration, supply that trust explicitly or retain the appropriate discovery policy instead of silently losing it.
No machine-wide proxy change is needed. Keep both the intended proxied host and a deliberate fallback host in your route checks.