Package: web3-proxy-providers
Proxy Providers for Web3PyA library for connecting to Web3 RPC providers using a proxyTested with:Python 3.6+Supports Proxy usage for:Http RPC (Sync, Async)Websocket RPC (Sync, Async)Websocket RPC with subscription (Sync, Async)Use the following command to install using pip:pip install web3-proxy-providersTo use the providers, you need web3 with version above 6.0.0, current version is 6.0.0b9 (beta)pip install web3==6.0.09bUsage exampleHttp Provider with ProxyUseHttpWithProxyProviderclass which supports http and socks proxyfromweb3importWeb3fromweb3_proxy_providersimportHttpWithProxyProviderprovider=HttpWithProxyProvider(endpoint_uri='https://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',proxy_url='socks5h://localhost:1080')web3=Web3(provider=provider,)print(web3.eth.block_number)Async Http Provider with ProxyUseAsyncHTTPWithProxyProviderclass to connect to an RPC with asyncio using a proxy. both http proxy and socks proxy are supportedimportasynciofromweb3importWeb3fromweb3.ethimportAsyncEthfrompython_socksimportProxyTypefromweb3_proxy_providersimportAsyncHTTPWithProxyProviderasyncdefmain():provider=AsyncHTTPWithProxyProvider(proxy_type=ProxyType.SOCKS5,proxy_host='localhost',proxy_port=1080,endpoint_uri='https://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',)web3=Web3(provider=provider,modules={'eth':(AsyncEth,)},)print(awaitweb3.eth.block_number)if__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(main())Async Websocket Provider with ProxyUseAsyncWebsocketWithProxyProviderclass to connect to a websocket RPC with asyncio using a proxy. both http proxy and socks proxy are supportedimportasynciofromweb3importWeb3fromweb3.ethimportAsyncEthfrompython_socksimportProxyTypefromweb3_proxy_providersimportAsyncWebsocketWithProxyProviderasyncdefmain(loop:asyncio.AbstractEventLoop):provider=AsyncWebsocketWithProxyProvider(loop=loop,proxy_type=ProxyType.SOCKS5,proxy_host='localhost',proxy_port=1080,endpoint_uri='wss://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',)web3=Web3(provider=provider,modules={'eth':(AsyncEth,)},)print(awaitweb3.eth.block_number)if__name__=='__main__':async_loop=asyncio.get_event_loop()async_loop.run_until_complete(main(loop=async_loop))Async Websocket Provider with Proxy with Subscription supportUseAsyncSubscriptionWebsocketWithProxyProviderclass to connect to a websocket RPC with asyncio using a proxy. both http proxy and socks proxy are supportedLearn more about realtime events and eth_subscribe:Ethereum/Geth DocsAlchemy DocsimportasynciofromCrypto.Hashimportkeccakfromweb3importWeb3frompython_socksimportProxyTypefromweb3_proxy_providersimportAsyncSubscriptionWebsocketWithProxyProviderasyncdefcallback(subs_id:str,json_result):print(json_result)asyncdefmain(loop:asyncio.AbstractEventLoop):provider=AsyncSubscriptionWebsocketWithProxyProvider(loop=loop,proxy_type=ProxyType.SOCKS5,proxy_host='localhost',proxy_port=1080,endpoint_uri='wss://eth-mainnet.g.alchemy.com/v2/<YourAlchemyKey>',)# subscribe to Deposit and Withdrawal events for WETH contractweth_contract_address=Web3.to_checksum_address('0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2')deposit_topic="0x"+keccak.new(data=b'Deposit(address,uint256)',digest_bits=256).hexdigest()withdrawal_topic="0x"+keccak.new(data=b'Withdrawal(address,uint256)',digest_bits=256).hexdigest()subscription_id=awaitprovider.subscribe(['logs',{"address":weth_contract_address,"topics":[deposit_topic,withdrawal_topic]}],callback)print(f'Subscribed with id{subscription_id}')# unsubscribe after 30 secondsawaitasyncio.sleep(30)awaitprovider.unsubscribe(subscription_id)if__name__=='__main__':async_loop=asyncio.get_event_loop()async_loop.run_until_complete(main(loop=async_loop))