I'm currently working on a project in which I have to implement a MQTT client.
I won't list the code for the whole class that implements the Connect Package as it's quite long, but the message it generates when converted to bytes is:
bytearray(b'\x10\x10\x00\x04MQTT\x05\x02\x00<\x00\x04digi')
I have tried sending the packet using socket:
connect_package = ConnectPacket(60,"digi")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1',1883))
s.sendall(connect_package.pack())
connect_package.pack()
returns the bytearray mentioned earlier
However, the mosquitto client responds in the following way:
1607186254: mosquitto version 2.0.0 starting
1607186254: Config loaded from .\mosquitto.conf.
1607186254: Opening ipv4 listen socket on port 1883.
1607186254: Opening ipv4 listen socket on port 1883.
1607186254: mosquitto version 2.0.0 running
1607186273: New connection from 127.0.0.1:58295 on port 1883.
1607186273: Client <unknown> disconnected due to protocol error.
I've tried to search for solutions but found nothing similar to my case.
I'm relatively new to Python so any critique is welcome, I'm here to learn.
Later edit:
this is the capture in Wireshark: https://i.sstatic.net/qlVRh.jpg
and this is the class: https://pastebin.com/LuGTARE6
The actual problem was that I have constructed my package without including the "Property Length" byte, so the broker was reading the last \x00 as Property Length, then it read \x04 as the MSB of the Client_ID length, so it was expecting a client with an id of at least 1124 bytes ( 00000100 -> 0x04 01100100 -> 'd'), but I was only providing 4. Many thanks to hardillb.