1006 Connection closed abnormally error with python 3.7 websockets

I ran in to this same issue. The solution by shinola worked for awhile, but I would still get errors sometimes.

To handle this I put the connection into a while True: loop and added two separate try except blocks. The consumer variable is a function that processes the messages received from the websocket connection.

async def websocketConnect(uri, payload, consumer):
    websocket = await websockets.connect(uri, ssl=True)
    await websocket.send(json.dumps(payload))
    while True:
        if not websocket.open:
            try:
                print('Websocket is NOT connected. Reconnecting...')
                websocket = await websockets.connect(uri, ssl=True)
                await websocket.send(json.dumps(payload))
            except:
                print('Unable to reconnect, trying again.')
        try:
            async for message in websocket:
                if message is not None:
                    consumer(json.loads(message))
        except:
            print('Error receiving message from websocket.')

I start the connection using:

def startWebsocket(uri, payload, consumer):
    asyncio.run(websocketConnect(uri, payload, consumer))

So I found the solution:

When the connection closes, it breaks out of the while loop for some reason. So in order to keep the websocket running you have to surround

resp = await websocket.recv()

with try ... except and have

print('Reconnecting')
websocket = await websockets.connect(ws_url)

in the exception handling part.


I might be a year late but i was just having this issue. No connection issues on my html5 websocket client but .py test client would crash after around a minute (raising 1006 exceptions for both the client and server too). As a test i started to just await connection.recv()ing after every frame the client sends. No more issues. I didnt need to receive data for my .py test client but apparently it causes issues if you let it build up. It's also probably why my web version was working fine, since i was handling the .onmessage callbacks.

Im pretty sure this is why this error occurs. So this solution of just receiving the data is an actual solution and not disabling pinging and screwing up a vital function of the protocol.


I encountered the same problem. After digging a while I found multiple versions of the answer that tells to just reconnect, but I didn't think it was a reasonable route, so I dug some more.

Enabling DEBUG level logging I found out that python websockets default to sending ping packets, and failing to receive a response, timeouts the connection. I am not sure if this lines up with the standard, but at least javascript websockets are completely fine with the server my python script times out with.

The fix is simple: add another kw argument to connect:

websockets.connect(uri, ping_interval=None)

The same argument should also work for server side function serve.

More info at https://websockets.readthedocs.io/en/stable/api.html