Analysis of Python protocol


1. Use the byte method to parse and package the protocol

bin_buff = bytes()
bin_buff += (0x1234).to_bytes(2, byteorder = 'big')
bin_buff += (0x56).to_bytes(1, byteorder = 'big')

print('0x%s' % bin_ buff.hex ()) ා output 0x123456
b1 = int.from_bytes(bin_buff[1:3], byteorder = 'big')
Print ('0x% X '% B1) ා output 0x3456

2. If you press struct in C, you can also use it

import struct
Import the struct module and use pack unpack
When writing a structure in the following format

struct {
    unsigned char a;
    unsigned short b;
    unsigned char c;

Follow the table below
Analysis of Python protocol

The format used for compression is BHb

print('0x%s' % struct.pack('BHB', 0x12, 0x3456, 0x78).hex())
Print out
Analysis of Python protocol
It seems to be to align 0x12, followed by a supplement of 0x00. How to achieve 1 byte alignment in C

Analysis of Python protocol

It’s not very clear to see this description. The effect is as follows

print('0x%s' % struct.pack('@BHB', 0x12, 0x3456, 0x78).hex())
print('0x%s' % struct.pack('=BHB', 0x12, 0x3456, 0x78).hex())
print('0x%s' % struct.pack('<BHB', 0x12, 0x3456, 0x78).hex())
print('0x%s' % struct.pack('>BHB', 0x12, 0x3456, 0x78).hex())
print('0x%s' % struct.pack('!BHB', 0x12, 0x3456, 0x78).hex())

Print out
Analysis of Python protocol

@As with nothing, it should be the default: fill alignment 4 bytes, host byte order.

And the rest= < > !It’s not filled

=It should be tested on X86 in byte order, so both and < are small end
and!Network byte order sum>They’re all arranged by the big end