How to find the size of mqtt data in python

Hi, I need to find the packet size of a mqtt data in python.

My question is, how can i find the mqtt packet size which include the control header, packet length, variable length header and payload .

At the moment i am using getsizeof to find it.But i am not sure whether it gives the payload alone or the complete size.

My data is in the form : b'{"0":294.3875741324,"1":292.1924140501}'
Size i get for this data with getsizeof is 72.

I hope somebody can help me.

Thanks in advance.

I'd ask this on a python forum or maybe the MQTT Google group https://groups.google.com/g/mqtt

Ok .Thank you for the link.

Try this Python snippet...

data = b'{"0":294.3875741324,"1":292.1924140501}'
string_data = data.decode('utf-8')  # Convert bytes to string using UTF-8 encoding
size = len(string_data)
print(size)  # Output: 39

Thank you for your reply .
Is it possible to use the same method for finding the size of the complete message .

If you post one of your typical, complete MQTT messages, I'll take a look.

Here's a simple example using part of a MQTT message from one of my weather stations.

readings = {
    "RCS_version": "v74D.py",
    "sensors": {"temp":21.6, "humidity":90, "pressure":1002},
    "hostname": "monday_test"
}

size = len(str(readings))
print("Size of the string representation of the dictionary: ", size)
Size of the string representation of the dictionary:  114

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.