python-使用struct模块实现16进制网络封包数据读写

import struct
class HexToolErroe(Exception):
    def __init__(self,errorinfo):
        super(HexToolErroe, self).__init__()
        self.errorinfo=errorinfo
    def __str__(self):
        return self.errorinfo


class hextool_write(object):
    def __init__(self,encoding='gbk',endianness='>'):
        super(hextool_write, self).__init__()
        self.encoding=encoding
        self.endianness=endianness
        self.data=b''

    def wint(self,value):
        self.data+=struct.Struct(self.endianness+'I').pack(value)
        # self.data += struct.pack(self.endianness + 'I', value)

    def wshort(self,value):
        self.data+=struct.Struct(self.endianness+'H').pack(value)


    def wbyte(self,value):
        self.data+=struct.Struct(self.endianness+'b').pack(value)


    def wstring_s(self,value):

        str = value.encode(self.encoding)
        slen = len(str)
        self.data+=struct.Struct(self.endianness+'b{}s'.format(slen)).pack(slen,str)


    def wstring_l(self, value):

        str=value.encode(self.encoding)
        slen = len(str)
        self.data += struct.Struct(self.endianness + 'H{}s'.format(slen)).pack(slen,str)


    def get_rawdata(self):
        return self.data
    def get_hexdata(self):
        return self.data.hex(' ').upper()
    def len(self):
        return len(self.data)


class hextool_read(object):
    def __init__(self,encoding='gbk',endianness='>'):
        super(hextool_read, self).__init__()
        self.encoding=encoding
        self.endianness=endianness
        self.data=b''
        self.index=0
        self.len=0

    def setdata(self,data):
        self.data=data
        self.len=len(data)
        self.index = 0

    def rint(self):
        if self.len-4<0:
            raise HexToolErroe("索引超出")
        (value,)= struct.unpack(self.endianness + 'I', self.data[self.index:self.index + 4])
        self.index += 4
        self.len -=4
        return value

    def rshort(self):
        if self.len-2<0:
            raise HexToolErroe("索引超出")
        (value,)= struct.unpack(self.endianness + 'H', self.data[self.index:self.index + 2])
        self.index += 2
        self.len -=2
        return value

    def rbyte(self):
        if self.len-1<0:
            raise HexToolErroe("索引超出")
        (value,)= struct.unpack(self.endianness + 'b', self.data[self.index:self.index + 1])
        self.index += 1
        self.len -= 1
        return value


    def rstring_l(self):
        slen = self.rshort()
        if self.len - slen < 0:
            raise HexToolErroe("索引超出")

        (value,) = struct.unpack(self.endianness + '{}s'.format(slen), self.data[self.index:self.index + slen])
        self.index += slen
        self.len -= slen
        return value.decode("gbk")

    def rstring_s(self):
        slen = self.rbyte()
        if self.len - slen < 0:
            raise HexToolErroe("索引超出")

        (value,) = struct.unpack(self.endianness + '{}s'.format(slen), self.data[self.index:self.index + slen])
        self.index += slen
        self.len-=slen
        return value.decode("gbk")
    
    def get_index(self):

        return  self.index
    def get_currentlen(self):

        return self.len

    def get_datalen(self):
        return len(self.data)



if __name__ == '__main__':
    x=hextool_write()
    x.wint(1)
    x.wstring_s('开心')
    x.wbyte(1)
    x.wstring_l('非常好')
    x.wshort(100)
    print(x.get_hexdata())

    y=hextool_read()
    y.setdata(x.get_rawdata())
    print('整数:',y.rint())
    print('短字符串:',y.rstring_s())
    print('字节:',y.rbyte())
    print('长字符串:',y.rstring_l())
    print('短整数:',y.rshort())
    print(y.get_currentlen())
    print(y.get_index())
    # print(y.rshort())

posted @ 2022-10-19 14:43  离人怎挽_wdj  阅读(161)  评论(0编辑  收藏  举报