I have a TCP client server communication. I’m sending string, and I would like to measure the size of the package, How can I do that?
In my case the sys.getsizeof is not good because
getsizeof() calls the object’s __sizeof__ method and adds an additional garbage collector
overhead if the object is managed by the garbage collector.
What is the size of one character, 2 or 4 byte? In this case I can simply measure as msg_size = x * len(msg), but I don’t know how many byte is x
In Python 2.x,
strobjects are 8-bit i.e. 1 byte per item. In 3.x you can usebytesobjects which again are 1 byte per item.For dealing with data, it’s usually better to use
bytearray; this is mutable and works the same in both 2.x and 3.x.