Most proxy problems in Python are not proxy problems. They come from the proxy URL: a password character that breaks parsing, a scheme that resolves DNS in the wrong place, an environment variable one library reads and another ignores. Our guide to using residential proxies with Python covers the basics. This post tests the edge cases. We ran 223 cases through requests, httpx and aiohttp against local test proxies that log exactly what each client sends.
Three findings matter most: socks5:// in requests resolves DNS on your machine, aiohttp ignores proxy environment variables by default, and *.example.com in NO_PROXY matches nothing.
SOCKS5: who resolves the hostname
A SOCKS5 client can send the proxy either a hostname or an IP address it has already looked up. If it looks up the address itself, the DNS query goes to your own resolver, outside the proxy, and the answer is chosen for your location, not the proxy’s.
| Client | socks5:// sends | socks5h:// sends |
|---|---|---|
| requests | IP address, resolved locally | Hostname |
| httpx | Hostname | Hostname |
| aiohttp with aiohttp-socks | Hostname | Error: invalid scheme |
With requests, socks5:// is a DNS leak: every lookup leaves from your own network, and sites behind a CDN may hand you an address near you rather than near the proxy. Use socks5h://. httpx always sent the hostname. aiohttp-socks sent the hostname with socks5:// and refused socks5h:// with ValueError: Invalid scheme component. Pass rdns=True to make remote resolution explicit.
Special characters in passwords
Proxy credentials go inside the URL, so characters that have a meaning in URLs can break it. We tested nine passwords, raw and percent-encoded, over HTTP and SOCKS5 proxies:
| Password contains | requests | httpx | aiohttp | Percent-encoded |
|---|---|---|---|---|
@ | Works | Works | Works | Works |
: | Works | Works | Works | Works |
# | URL error | URL error | URL error | Works |
/ | URL error | URL error | URL error | Works |
? | URL error | URL error | URL error | Works |
% alone | Works | Works | Works | Works |
| Space | Works | Works | Works | Works |
%40 literally | Wrong password | Wrong password | Wrong password | Works |
The URL errors are at least loud: requests reports InvalidURL: Failed to parse, httpx Invalid port, aiohttp InvalidURL. The %40 case is silent. If your real password contains the text %40, every client decodes it to @ and sends the wrong password, and the proxy answers with a 407 authentication error that looks like a bad account.
The reason is URL syntax. Under RFC 3986, the credentials sit in the “userinfo” part, before the last @. A # starts the fragment, a ? starts the query and a / starts the path, so the parser ends the address at that character and reads what is left, here the start of your password, as the host and port. That is why the errors talk about an invalid port rather than a bad password. An @ or : survives because all three clients split credentials at the last @ and the first :, but that is a parser convenience, not something to rely on. A % followed by two hex digits is a percent-escape by definition, which is why %40 becomes @.
Percent-encoding fixed every ASCII case in every client:
from urllib.parse import quote
user = quote(os.environ["PROXY_USER"], safe="")
password = quote(os.environ["PROXY_PASS"], safe="")
proxy = f"http://{user}:{password}@PROXY_HOST:PORT"
Non-ASCII passwords are less predictable. Over an HTTP proxy, requests and aiohttp sent a password containing ä encoded as Latin-1 and httpx as UTF-8, so the same password reaches the proxy as different bytes depending on the library. Over SOCKS5, aiohttp-socks failed with a UnicodeEncodeError. Keep proxy passwords to ASCII letters, digits and simple symbols.
Environment variables
HTTP_PROXY, HTTPS_PROXY, ALL_PROXY and NO_PROXY let you set a proxy without touching code. The three clients do not read them the same way:
| Setting | requests | httpx | aiohttp |
|---|---|---|---|
HTTP_PROXY and HTTPS_PROXY | Used | Used | Ignored unless trust_env=True |
Lowercase http_proxy and https_proxy | Used | Used | Same as above |
ALL_PROXY=socks5h://... | Used | Used | Ignored, even with trust_env=True |
| Proxy passed in code, NO_PROXY matches | Proxy used | Proxy used | Proxy used |
The aiohttp default is the risky one. A script that works through the proxy with requests can be rewritten with aiohttp, and the same environment then sends every request directly from your own IP, with no error. Pass trust_env=True, or better, pass the proxy explicitly.
NO_PROXY lists hosts that should skip the proxy. For a request to site.example.test, with aiohttp set to trust_env=True:
| NO_PROXY value | Skips the proxy? |
|---|---|
example.test | Yes, in all three |
.example.test | Yes, in all three |
site.example.test | Yes, in all three |
EXAMPLE.TEST | Yes, in all three |
* | Yes, in all three |
*.example.test | No, in all three |
example | No, in all three |
The wildcard row is the trap. *.example.test looks like the obvious way to exclude a domain, but no client treats it as a pattern, so internal or excluded traffic keeps going through the proxy and is billed as proxy bandwidth.
A setup that works everywhere
- Build the URL in code from environment variables, percent-encoding the username and password.
- Use
socks5h://with requests. With aiohttp-socks, usesocks5://andrdns=True. - Pass the proxy explicitly rather than relying on environment variables, so a library change cannot silently remove it.
- Write NO_PROXY entries as domains,
example.comor.example.com, never*.example.com. - Check your exit IP once at startup. The async proxy checker we built earlier shows a simple way.
For the choice between the two protocols, see HTTP vs SOCKS5 proxies. Shifter’s residential proxies support both HTTP and SOCKS5.
Debugging a proxy URL
When a proxy works in curl but not in Python, or in one library but not another, check these three things, in this order. First, print the parsed URL without the password: urllib.parse.urlsplit(proxy) shows the host and port the library will use, and a wrong port or a host that looks like part of your password points to an unencoded character. Second, check which proxy, if any, the library picked: in requests, requests.utils.get_environ_proxies(url) returns what it will take from the environment, and in aiohttp, remember that nothing is taken from the environment unless you asked for it. Third, compare the exit IP and the DNS location with what you expect. If the IP is the proxy’s but the DNS resolver is your own, you are on socks5:// with requests.
How we tested
On 30 November 2026 we ran local test proxies on 127.0.0.1, one SOCKS5 and two HTTP, that answered every request themselves and logged the target, the address type and the credentials received. No request left the machine through them. We tested Python 3.11.10 with requests 2.34.2 (urllib3 2.8.0, PySocks 1.7.1), httpx 0.29.0 (socksio 1.0.0) and aiohttp 3.14.5 (aiohttp-socks 0.12.0): two SOCKS schemes, nine passwords raw and percent-encoded over HTTP and SOCKS5, and eleven environment setups for HTTP and HTTPS targets. Each case ran in a fresh process with all other proxy variables removed.
The bottom line
Percent-encode proxy credentials, use socks5h:// with requests, and never trust NO_PROXY wildcards. Above all, do not assume a library reads your proxy environment variables: aiohttp does not, by default, and a script that silently goes direct is the hardest proxy mistake to notice.
Sources and references
- requests documentation, Proxies and SOCKS: requests.readthedocs.io
- HTTPX documentation, Proxies: python-httpx.org
- HTTPX documentation, Environment variables: python-httpx.org
- aiohttp documentation, Proxy support: docs.aiohttp.org
- aiohttp-socks: github.com
- IETF, RFC 1928, SOCKS Protocol Version 5: rfc-editor.org