feat: add SOCKS proxy support via aiohttp-socks ProxyConnector
MA
5mo ago+65+65+01234567890123456789-75-75-01234567890123456789
fixes #23797
This PR adds SOCKS proxy support to the asyncio Python client template.
When a kubeconfig cluster entry contains a proxy-url with a SOCKS
scheme (e.g. socks5://localhost:1080), the async client currently
fails with:
aiohttp.client_exceptions.ServerDisconnectedError: Server disconnected
This happens because aiohttp.TCPConnector does not support SOCKS
schemes at the request level — a dedicated ProxyConnector from
aiohttp-socks is required at the session level instead.
Changes
modules/openapi-generator/src/main/resources/python-asyncio/rest.mustache
- Add
SUPPORTED_SOCKS_PROXIESfrozenset constant for the four SOCKS
schemes (socks4,socks4a,socks5,socks5h) - Detect SOCKS scheme via
partition("://")— no extra dependencies - Route SOCKS proxies to
aiohttp_socks.ProxyConnectorinstead of
aiohttp.TCPConnector - Lazy import of
ProxyConnectorso non-SOCKS users are unaffected - Set
self.proxy = Nonewhen using SOCKS connector to avoid double
proxy application per-request
Notes
- HTTP/HTTPS proxy code path completely unchanged
- All four SOCKS schemes supported:
socks4,socks4a,socks5,socks5h - Requires
aiohttp>=3.10.0andaiohttp-socks>=0.9.0 - Complements the urllib3 SOCKS fix for the sync Python client template
- Non-SOCKS users are completely unaffected —
aiohttp-socksis never
imported unless a SOCKS proxy URL is configured - Users with a SOCKS proxy who don't have
aiohttp-socksinstalled will
receive a clearImportErrorinstead of the previous cryptic
ServerDisconnectedError self.proxyis set toNonewhen usingProxyConnectorto avoid
double proxy application — the connector handles routing at the
connection level, not per-request
PR checklist
- Read the contribution guidelines.
- Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
- Run the following to build the project and update samples:
- File the PR against the correct branch:
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks) - If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having
"fixes #123"present in the PR description) - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Summary by cubic
Add SOCKS proxy support to the asyncio Python client via aiohttp-socks ProxyConnector, fixing failures when proxy-url uses socks* schemes. Fixes #23797.
-
New Features
- Support
socks4,socks4a,socks5,socks5hvia session-levelProxyConnector.from_urlwithtrust_env=True; HTTP/HTTPS path unchanged; requiresaiohttp-socks>=0.9.0. - Auto-detect
socks*and create theaiohttpClientSessionwithProxyConnectorvsTCPConnector; for SOCKS, disable per-requestproxy/proxy_headers.
- Support
-
Refactors
- Sync template: use
partition("://")and afrozensetfor SOCKS detection; updated generated samples. - Async template: build the session inside the async context and select connector by scheme; removed unnecessary comments.
- Sync template: use