httpx
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?
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)
- Docs: [requests.get() - Requests Documentation](https://docs.python-requests.org/en/latest/api/#requests.get)
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
- Docs: [AsyncClient - HTTPX Documentation](https://www.python-httpx.org/api/#asyncclient)
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())
- Docs: [Proxy Support - aiohttp Documentation](https://docs.aiohttp.org/en/stable/client_advanced.html#proxy-support)
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://andsocks5://proxy schemes inaiohttpandhttpx - 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_socksProxyConnector.
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!
- Docs: [Webshare API Docs](https://apidocs.webshare.io/)