Пример #1
0
def read(sock, data):
    try:
        """
        Analysis command
        @param sock the socket of client
        @param data the read data
        """
        read_count = 0
        code = int(unpack("<H", data[0:2])[0])
        read_count += 2
        cmd = Command(code)
        num_arg = int(unpack("<H", data[2:4])[0])
        read_count += 2
        for i in range(0, num_arg, 1):
            """Read all argument"""
            arg_code = int(unpack("<H", data[read_count:read_count+2])[0])
            read_count += 2
            #Argument type
            arg_type = int(unpack("B", data[read_count:read_count + 1])[0])
            read_count += 1
            if arg_type == Argument.STRING:
                #string len
                str_len = int(unpack("<I", data[read_count:read_count + 4])[0])
                read_count += 4
                str_val = str(unpack(str(str_len)+"s", data[read_count:read_count + str_len])[0])
                read_count += str_len
                cmd.add_string(arg_code, str_val)
            elif arg_type == Argument.RAW:
                raw_len = int(unpack("<I", data[read_count:read_count + 4])[0])
                read_count += 4
                raw_data = data[read_count, read_count + raw_len]
                read_count += raw_len
                cmd.add_raw(arg_code, raw_data)
            elif arg_type == Argument.BYTE:
                byte_val = int(unpack("B", data[read_count:read_count + 1])[0])
                read_count += 1
                cmd.add_byte(arg_code, byte_val)
            elif arg_type == Argument.SHORT:
                short_val = int(unpack("<H", data[read_count:read_count + 2])[0])
                read_count += 2
                cmd.add_short(arg_code, short_val)
            elif arg_type == Argument.INT:
                int_val = int(unpack("<i", data[read_count:read_count + 4])[0])
                read_count += 4
                cmd.add_int(arg_code, int_val)
            elif arg_type == Argument.LONG:
                long_val = long(unpack("<L", data[read_count:read_count + 8])[0])
                read_count += 8
                cmd.add_long(arg_code, long_val)
        analysis_message(sock, cmd)
    except Exception:
        pass
    pass
Пример #2
0
 def read(self):
     read_count = 0
     code = int(unpack("<H", self.data[0:2])[0])
     read_count += 2
     cmd = Command(code)
     num_arg = int(unpack("<H", self.data[2:4])[0])
     read_count += 2
     for i in range(0, num_arg, 1):
         #Read all argument
         arg_code = int(unpack("<H", self.data[read_count:read_count+2])[0])
         read_count += 2
         #Argument type
         arg_type = int(unpack("B", self.data[read_count:read_count + 1])[0])
         read_count += 1
         if arg_type == Argument.STRING:
             #string len
             str_len = int(unpack("<I", self.data[read_count:read_count + 4])[0])
             read_count += 4
             str_val = str(unpack(str(str_len)+"s", self.data[read_count:read_count + str_len])[0])
             read_count += str_len
             cmd.add_string(arg_code, str_val)
         elif arg_type == Argument.RAW:
             raw_len = int(unpack("<I", self.data[read_count:read_count + 4])[0])
             read_count += 4
             raw_data = self.data[read_count, read_count + raw_len]
             read_count += raw_len
             cmd.add_raw(arg_code, raw_data)
         elif arg_type == Argument.BYTE:
             byte_val = int(unpack("B", self.data[read_count:read_count + 1])[0])
             read_count += 1
             self.add_byte(arg_code, byte_val)
         elif arg_type == Argument.SHORT:
             short_val = int(unpack("<H", self.data[read_count:read_count + 2])[0])
             read_count += 2
             cmd.add_short(arg_code, short_val)
         elif arg_type == Argument.INT:
             int_val = int(unpack("<I", self.data[read_count:read_count + 4])[0])
             read_count += 4
             cmd.add_int(arg_code, int_val)
         elif arg_type == Argument.LONG:
             long_val = long(unpack("<L", self.data[read_count:read_count + 8])[0])
             read_count += 8
             cmd.add_long(arg_code, long_val)
     print cmd.get_log()