1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import asyncio import json import aiohttp
blocknumber = 11
async def ws(): global blocknumber session = aiohttp.ClientSession() async with session.ws_connect('wss://bsc-mainnet.nodereal.io/ws/v1/***') as ws: await ws.send_str('{"jsonrpc": "2.0", "id": 1, "method": "eth_subscribe", "params": ["logs"]}') async for msg in ws: try: _blocknumber = int(json.loads(msg.data).get("params").get("result").get("blockNumber"), 16) if blocknumber != _blocknumber: print(blocknumber) blocknumber = _blocknumber except Exception as e: pass
if __name__ == '__main__': asyncio.run(ws())
|