httpx icon indicating copy to clipboard operation
httpx copied to clipboard

Why does SOCKS5 proxy authentication work in `requests` and `curl`, but fail in `httpx` and `aiohttp` with a 407 Proxy Authentication Required error?

Open kgtl12 opened this issue 6 months ago • 1 comments
trafficstars

Description:

I'm trying to make HTTP requests through a SOCKS5 proxy with username/password authentication.


Working (synchronous requests):

This works fine using the requests library:

import requests

proxies = {
    "http": "socks5://<username>:<password>@p.webshare.io:80",
    "https": "socks5://<username>:<password>@p.webshare.io:80"
}

response = requests.get(
    url="https://httpbin.org/ip",
    headers={"User-Agent": "MyAgent"},
    proxies=proxies,
    timeout=10
)

print(response.text)

Not working (async httpx):

With the async httpx client, I get a 407 Proxy Authentication Required error:

import httpx

proxy_url = "socks5://<username>:<password>@p.webshare.io:80"

async with httpx.AsyncClient(proxies=proxy_url, timeout=10) as client:
    response = await client.get(
        url="https://httpbin.org/ip",
        headers={"User-Agent": "MyAgent"},
    )

The error is:

httpx.ProxyError: [Errno 407] Proxy Authentication Required

Also fails with aiohttp:

Even when using aiohttp and passing proxy_auth, I still get authentication issues:

import aiohttp

auth = aiohttp.BasicAuth("<username>", "<password>")

async with aiohttp.ClientSession() as session:
    async with session.get(
        url="https://httpbin.org/ip",
        proxy="http://p.webshare.io:80",
        proxy_auth=auth
    ) as resp:
        print(await resp.text())

Tested via cURL (also works):

curl --proxy "socks5://<username>:<password>@p.webshare.io:80/" https://httpbin.org/ip

This works and returns the proxy IP.


What I've Tried:

  • Confirmed credentials are correct (works with cURL and requests)
  • Tried both http:// and socks5:// proxy schemes in aiohttp and httpx
  • Checked for whitespace or malformed proxy strings
  • Looked into environment variables (HTTP_PROXY, HTTPS_PROXY)
  • Tried rotating IPs and ports on the provider side
  • Tried aiohttp_socks ProxyConnector.

Question:

Why does this SOCKS5 proxy with authentication work in requests and curl, but fail in both httpx and aiohttp with a 407 Proxy Authentication Required error?

Any guidance or insights on async-compatible SOCKS5 proxy usage with auth would be appreciated!

kgtl12 avatar Apr 24 '25 09:04 kgtl12