qemu.aqmp.message module

QMP Message Format

This module provides the Message class, which represents a single QMP message sent to or from the server.

class qemu.aqmp.message.Message(value: Union[bytes, Mapping[str, object]] = b'{}', *, eager: bool = True)[source]

Bases: MutableMapping[str, object]

Represents a single QMP protocol message.

QMP uses JSON objects as its basic communicative unit; so this Python object is a MutableMapping. It may be instantiated from either another mapping (like a dict), or from raw bytes that still need to be deserialized.

Once instantiated, it may be treated like any other MutableMapping:

>>> msg = Message(b'{"hello": "world"}')
>>> assert msg['hello'] == 'world'
>>> msg['id'] = 'foobar'
>>> print(msg)
{
  "hello": "world",
  "id": "foobar"
}

It can be converted to bytes:

>>> msg = Message({"hello": "world"})
>>> print(bytes(msg))
b'{"hello":"world","id":"foobar"}'

Or back into a garden-variety dict:

>>> dict(msg)
{'hello': 'world'}
Parameters
  • value – Initial value, if any.

  • eager – When True, attempt to serialize or deserialize the initial value immediately, so that conversion exceptions are raised during the call to __init__().

exception qemu.aqmp.message.DeserializationError(error_message: str, raw: bytes)[source]

Bases: qemu.aqmp.error.ProtocolError

A QMP message was not understood as JSON.

When this Exception is raised, __cause__ will be set to the json.JSONDecodeError Exception, which can be interrogated for further details.

Parameters
  • error_message – Human-readable string describing the error.

  • raw – The raw bytes that prompted the failure.

raw: bytes

The raw bytes that were not understood as JSON.

error_message: str

Human-readable error message, without any prefix.

exception qemu.aqmp.message.UnexpectedTypeError(error_message: str, value: object)[source]

Bases: qemu.aqmp.error.ProtocolError

A QMP message was JSON, but not a JSON object.

Parameters
  • error_message – Human-readable string describing the error.

  • value – The deserialized JSON value that wasn’t an object.

error_message: str

Human-readable error message, without any prefix.

value: object

The JSON value that was expected to be an object.