Skip to content
Advertisement

Retrieving unpack requires a buffer of 8 bytes error socket python

I have the following code I am sending the result of intermediate predictions results from the client to server.

Client

JavaScript

server

JavaScript

While running the above code I am facing the below error

msg_size = struct.unpack(“Q”, packed_msg_size)[0] struct.error: unpack requires a buffer of 8 bytes

Thank you

Advertisement

Answer

You haven’t sorted out normal end of connection. As mentioned in the comments, when the connection is closed by the client, you hit if not packet: break but that only breaks you out of the inner loop and you keep on trying to process a non-existent message. There is another bug in there where you don’t clear out data per message.

When nesting loops, especially in communications where end of processing is discovered several steps down, I find it best to break the work into multiple functions with more narrowly defined roles. Here, you want to receive exactly one message at a time, build that into a python object and then use it. 3 functions for three things.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement