Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncClient doesn't read proxy settings from environment variables while HTTPClient does #3477

Open
sparkTesal opened this issue Jan 16, 2025 · 2 comments

Comments

@sparkTesal
Copy link

sparkTesal commented Jan 16, 2025

Issue Description

When using httpx.AsyncClient, it doesn't automatically read proxy settings from environment variables, while httpx.Client does. This leads to inconsistent behavior between sync and async clients.

Reproduction Steps

Here's a minimal example demonstrating the issue:

import httpx
import asyncio
import os

# Set proxy environment variables
os.environ["HTTPS_PROXY"] = "socks5://127.0.0.1:13659"
os.environ["ALL_PROXY"] = "socks5://127.0.0.1:13659"

# Test sync client
def test_sync():
    with httpx.Client() as client:
        response = client.get("http://httpbin.org/ip")
        print(f"Sync client IP: {response.text}")

# Test async client
async def test_async():
    async with httpx.AsyncClient() as client:
        response = await client.get("http://httpbin.org/ip")
        print(f"Async client IP: {response.text}")

# Run tests
print("Testing sync client:")
test_sync()

print("\nTesting async client:")
asyncio.run(test_async())

Current Behavior

  • Sync client (httpx.Client) correctly uses the proxy from environment variables
  • Async client (httpx.AsyncClient) ignores environment variables and makes direct connections

Expected Behavior

Both sync and async clients should respect proxy settings from environment variables consistently.

Workarounds

Currently, we have to explicitly set proxies for AsyncClient to work:

proxies = {
    "http://": "socks5://127.0.0.1:13659",
    "https://": "socks5://127.0.0.1:13659"
}
async with httpx.AsyncClient(proxies=proxies) as client:
    # ... make requests

Environment

  • Python version: 3.11
  • httpx version: [ 0.27.2]
  • OS: [OSX]

Additional Context

This inconsistency can cause issues in libraries that use httpx's AsyncClient internally, as users might expect environment variables to work the same way as they do with the sync client.

@Parthib
Copy link

Parthib commented Jan 16, 2025

+1

I was running into an issue where a dependent package was not picking up on my ALL_PROXY env var, and this is likely why.

Any ideas why these two clients behave differently? I can't find many references to these env vars other than this commit: 49ed77a

@Parthib
Copy link

Parthib commented Jan 18, 2025

Actually, I just ran your script with a proxy and got the public ip address of my proxy server:

Testing sync client:
Sync client IP: {
  "origin": "35.165.132.134"
}


Testing async client:
Async client IP: {
  "origin": "35.165.132.134"
}

Are you seeing different IP addresses here? I am using version 0.27.2 here as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants