3

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?

10
  • 1
    The "sockets" or "socket" module? Commented Feb 7, 2018 at 23:25
  • 1
    Isn't the structmodule useful for this? Commented Feb 7, 2018 at 23:26
  • @MustacheMoses Yes, thanks for noticing, it's 'socket' Commented Feb 7, 2018 at 23:26
  • "sys.getsizeof" does not check the length of the bytes, it checks the size of the bytes object. If you want the length, you need len(byte_object) Commented Feb 7, 2018 at 23:27
  • @juanpa.arrivillaga you're, right, I made a fix in description Commented Feb 7, 2018 at 23:29

1 Answer 1

5

bytes can be tricky.

First off, encoded_array = str(int_array).encode() is not doing what you think. If you print encoded_array, you'll see that it's literally converting the to_string value of int_array to bytes. This is why the first value of encoded_array is [

>>> encoded_array[:1]
b'['

Second, I'm not sure you want int_array = [i**2 for i in range(1, 100)] to do what it's doing. It creates values up to 10,000. I'm unsure if you would like for this range to be between 0 and 256 if each element is to represent a byte. I'm going to assume you would like some data to be converted to bytes and split into 40 byte chunks and the data in this case is an array of integers.

First let's convert your int_array into an array of bytes. I'm going to convert each int into a 2 byte value and represent it in hex.

>>> hex_array = [x.to_bytes(2, byteorder="big") for x in int_array]

Now to split up the data into bytes of 40

>>> h
[]

>>> for x in range(0, int(round(len(hex_array)/20))):
...     h.append(hex_array[:20])
...     del hex_array[:20]

I'm splitting by 20 because each element holds 2 bytes

Now you'll have an array of max 40 byte collections. Time to join the collections together so we can transmit them!

>>> result
[]

>>> for package in h:
...     joined_package = b';'.join(package)
...     result.append(joined_package)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.