[Python] websockets.exceptions 4040 (private use), reason = Draining connection --python

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I keep getting this error on my websocket connection

    websockets.exceptions.ConnectionClosedError: code = 4040 (private use), reason = Draining connection


    This is my code

    bsc = 'wss://speedy-nodes-nyc.moralis.io/123456789/bsc/mainnet/ws'
    web3 = Web3(Web3.WebsocketProvider(bsc))
    print(web3.isConnected())

    def handle_event(event):
    #Web3.toJSON(event)
    print(Web3.toJSON(event))



    async def log_loop(event_filter, poll_interval):
    while True:
    for event in event_filter.get_new_entries():
    handle_event(event)
    await asyncio.sleep(poll_interval)

    def main():
    event_filter = myContract.events.Transfer.createFilter(fromBlock="latest")
    #block_filter = web3.eth.filter('latest')
    #tx_filter = web3.eth.filter('pending')
    loop = asyncio.get_event_loop()
    try:
    loop.run_until_complete(
    asyncio.gather(
    #log_loop(block_filter, 2),
    #log_loop(tx_filter, 2)))
    log_loop(event_filter, 2)))
    finally:
    loop.close()

    if __name__ == '__main__':
    main()


    How can I automatically reconnect to my websocketprovider if the connection closes without exiting my running script?

    I know there have been answers to similar questions but none of them work for me.

    I also know that it works when adding options in Javascript to the websocketprovider

    reconnect: {
    auto: true,
    delay: 10000,
    maxAttempts: 10,
    }


    How would I use these options in python?

    Continue reading...

Compartilhe esta Página