Skip to content

Conversation

@lovelydinosaur
Copy link
Contributor

@lovelydinosaur lovelydinosaur commented Aug 2, 2020

If either app=... or transport=... are passed explicitly, then no env-var proxies should be used.

Closes #1039

Eg.

# Disregard any `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY` environment settings in these contexts...
client = httpx.Client(app=my_flask_app)
client = httpx.Client(transport=httpx.AsyncTransport(my_starlette_app)

@lovelydinosaur lovelydinosaur added the bug Something isn't working label Aug 2, 2020
@lovelydinosaur lovelydinosaur added this to the v0.14 milestone Aug 2, 2020
Copy link
Contributor

@florimondmanca florimondmanca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicey!

@lovelydinosaur lovelydinosaur merged commit 0e7730b into master Aug 2, 2020
@lovelydinosaur lovelydinosaur deleted the setting-app-or-transport-disregards-proxies branch August 2, 2020 11:42
@lovelydinosaur lovelydinosaur mentioned this pull request Aug 6, 2020
@MarkWine
Copy link

MarkWine commented Dec 15, 2023

Sorry to pop this back up, and this probably just reflects may lack of understanding of proxies, but I was wondering why using a transport also disregards the proxy. Looking at the requests HTTPAdapter (which seems to be similar in most regards), environmental proxies are maintained, so in the following example:

import asyncio
import os

import httpx
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util import Retry

os.environ["https_proxy"] = "http://127.0.0.1:8080/"

async def test_transport_proxy():
    base_client = httpx.AsyncClient()
    transport_client = httpx.AsyncClient(transport=httpx.AsyncHTTPTransport(retries=2))
    await base_client.get("https://example.org/1")
    await transport_client.get("https://example.org/2")


def test_adapter_proxy():
    base_session = Session()
    transport_session = Session()
    transport_session.mount("https://", HTTPAdapter(max_retries=Retry(total=3)))
    base_session.get("https://example.org/3")
    transport_session.get("https://example.org/4")


asyncio.run(test_transport_proxy_a())
test_adapter_proxy()

The proxy is used in all but case 2. I ran into this when we migrated an app to an environment needing proxies, and found everything worked based on environment proxy settings except the one case in which we added retries when making a large number of calls to a somewhat flaky external service. It was straightforward to add proxy rules directly, so no complaints per se, but I was wondering on the rationale behind this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASGI requests are routed through proxy

4 participants