Skip to content

Setting app=... or transport=... should bypass environment proxies. - #1122

Merged
lovelydinosaur merged 2 commits into
masterfrom
setting-app-or-transport-disregards-proxies
Aug 2, 2020
Merged

Setting app=... or transport=... should bypass environment proxies.#1122
lovelydinosaur merged 2 commits into
masterfrom
setting-app-or-transport-disregards-proxies

Conversation

@lovelydinosaur

@lovelydinosaurlovelydinosaur commented Aug 2, 2020

Copy link
Copy Markdown
Contributor

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)

@lovelydinosaurlovelydinosaur added the bug Something isn't working label Aug 2, 2020
@lovelydinosaurlovelydinosaur added this to the v0.14 milestone Aug 2, 2020
j178
j178 approved these changes Aug 2, 2020

@florimondmancaflorimondmanca left a comment

Copy link
Copy Markdown
Contributor

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 masterAug 2, 2020
@lovelydinosaur
lovelydinosaur deleted the setting-app-or-transport-disregards-proxies branch August 2, 2020 11:42
@lovelydinosaurlovelydinosaur mentioned this pull request Aug 6, 2020
@MarkWine

MarkWine commented Dec 15, 2023

Copy link
Copy Markdown

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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugSomething isn't working

Development

Successfully merging this pull request may close these issues.

ASGI requests are routed through proxy

4 participants

@lovelydinosaur@MarkWine@j178@florimondmanca