code: def write_message(self, msg, timeout=None):
      
      replace_dict = {'command': self.CMD_TO_WIRE[msg.command]}
      if msg.has_data:
        # Swap out data for the data length for the wire.
        data = msg[-1]
        replace_dict[msg._fields[-1]] = len(data)
  
      self.stream.write(struct.pack(msg.struct_format,
                                    *msg._replace(**replace_dict)), timeout)
      if msg.has_data:
        self.stream.write(data, timeout)
docs: Write an arbitrary message (of one of the types above).
  
      For the host side implementation, this will only ever be a DataMessage, but
      it's implemented generically enough here that you could use
      FilesyncTransport to implement the device side if you wanted.
  
      Args:
        msg:  The message to send, must be one of the types above.
        timeout: timeouts.PolledTimeout to use for the operation.