"binascii.Error: Incorrect padding" when receiving binary message from Swift SocketIO client #1354
-
I am using python SocketIO as a server, and I will transmit images from the client to the server. The images will be binary data in PNG format. When the python server receives such messages from a Swift client, the following error presents:
I think this error is triggered when the server handles this event. It automatically pops up when the message arrives at the server. Then, it automatically stops the connection. However, I also tried forging an image data in a test python SocketIO client. This time, no error is shown. My Swift code to generate binary PNG data (of type Image(uiImage: myUIImageVariable).pngData()
// Also handled the case when `pngData` returns `nil` My python code to generate binary PNG data (of type imageBytesIO = BytesIO()
image = Image.new("RGB", (244, 244), "#B99169")
draw = ImageDraw.Draw(image)
draw.text((10, 10), f"Time: {t}", fill="black")
image.save(imageBytesIO, format="PNG")
imageBytes = imageBytesIO.getvalue()
imageBytesIO.close()
# imageBytes is the final value I am using python-socketio version 5.11.2, and Swift SocketIO 16.1.0. I assume they are compatible according to this post. The Swift SocketIO library mentions "version three", which I think should be the JS SocketIO version instead of the protocol version. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
It appears the Swift port of Socket.IO is encoding base64 binary data without standard padding. I've found a stack overflow answer that suggests that the base64 encoder in Swift does not generate padding, even though this is the standard. The base64 spec says this:
The Socket.IO protocol does not explicitly indicate if padding must be used or not, but the example included in the specification does use padding. |
Beta Was this translation helpful? Give feedback.
It appears the Swift port of Socket.IO is encoding base64 binary data without standard padding. I've found a stack overflow answer that suggests that the base64 encoder in Swift does not generate padding, even though this is the standard. The base64 spec says this:
The Socket.IO protocol does not explicitly indicate if padding must be used or not, but the example included in the specification does use padding.