close

[Solved] 1006 Connection closed abnormally error with python 3.7 websockets

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error 1006 Connection closed abnormally error with python 3.7 websockets in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How 1006 Connection closed abnormally error with python 3.7 websockets Error Occurs?

Today I get the following error 1006 Connection closed abnormally error with python 3.7 websockets in python.

How To Solve 1006 Connection closed abnormally error with python 3.7 websockets Error ?

  1. How To Solve 1006 Connection closed abnormally error with python 3.7 websockets Error ?

    To Solve 1006 Connection closed abnormally error with python 3.7 websockets Error 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.

  2. 1006 Connection closed abnormally error with python 3.7 websockets

    To Solve 1006 Connection closed abnormally error with python 3.7 websockets Error 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.

Solution 1

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.

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read