Python Parse Minecraft Packet
Question
I have a script that connects to a mine craft server, receives packets, and sends packets.
So, I send a 'login' packet, and I receive a 'login' packet. Unfortunately, the received login packet is encoded (Information about encoding here: http://wiki.vg/Protocol#0x01).
The received login packet is stored in a variable named received_login_packet
. I need to decode it so that I can get the separate bits of data, such as the packet type, the dimension, etc...
I've looked around a bit, but I have absolutely no idea as to how to go about doing this.
Here's some code if it helps:
#encoding the packet to send
encuserlen = str(len(enc_user)) # enc_user is just my username
packfmt = '>bih%sshiibBB' % encuserlen
packetbytes = struct.pack(packfmt, 1, 28, len(data['user']), enc_user, 0, 0, 0, 0, 0, 0)
s.send(packetbytes)
time.sleep(2)
#login packet sent, waited for response
response = s.recv(1024) #this is the raw login response.
#it's encoded as detailed above. i want to decode it
Any help would be appreciated and please don't hesitate to say if it's not clear enough.
Solution
So, if I understand this right, you want to decode the packet response, which is a 1024 byte, to get the correct information out. It seems you were able to use the struct.pack statement, there is a similar statement to unpack, as seen in the documentation. Basically, it looks like this.
packfmt = '>issiibBB'
output=struct.unpack(packfmt,response)
Also, I'm not quite convinced that your request was being sent right, but I'll leave that as an exercise for you to figure out how to set it. See format strings.