web3.py Proxy Settings: HTTPProvider vs. AsyncHTTPProvider
Use Requests-style proxies for HTTPProvider and aiohttp-style proxy for AsyncHTTPProvider instead of copying the same request_kwargs between them.
In web3.py 7.14, HTTPProvider uses Requests while AsyncHTTPProvider uses aiohttp. Their request_kwargs reach different HTTP clients.
For a synchronous HTTPS JSON-RPC endpoint:
from web3 import Web3
client = Web3(Web3.HTTPProvider(
rpc_url,
request_kwargs={
'proxies': {'https': 'http://proxy.example:8080'},
'timeout': 20,
},
))
Keep rpc_url as the actual node endpoint. The map’s value names the intermediary. For a public RPC node on a permitted public-proxy route, the HTTP proxy list supplies candidates for the HTTP proxy values in either example.
The asynchronous provider instead uses singular proxy with a URL value:
from aiohttp import ClientTimeout
from web3 import AsyncHTTPProvider, AsyncWeb3
provider = AsyncHTTPProvider(
rpc_url,
request_kwargs={
'proxy': 'http://proxy.example:8080',
'timeout': ClientTimeout(total=20),
},
)
async_client = AsyncWeb3(provider)
Copying proxies into the asynchronous path produces an unexpected-keyword error in aiohttp. Both the spelling and the value type need to change.
Close the asynchronous provider with await provider.disconnect() in its cleanup path. These settings belong to the HTTP providers; a WebSocket provider has its own transport interface.