I need to send an array by 'packages' of specific length (say 40 bytes) using TCP Protocol and Python's socket module.
First I'm generating sample array of ints:
int_array = [i**2 for i in range(1, 100)]
Then I'm converting it into bytes object:
encoded_array = str(int_array).encode()
To check length of encoded_array object I'm using len(encoded_array) which in this particular case equals 551 bytes.
Say I want to sent packages that are 40 bytes long: this makes full 13 packages and the leftovers from last bytes of encoded_array, total of 14 packages. But I can't figure out the method how to divide bytes object into smaller bytes objects by number of bytes itself. Does anyone have an idea how to do this?
I'm also aware that I may be doing it in totally wrong way, so please advise me on how to send 'portioned' data via TCP protocol. Maybe there's no need to do this splitting at all?
structmodule useful for this?len(byte_object)