1 # QEMU Monitor Protocol Python class
3 # Copyright (C) 2009 Red Hat Inc.
8 # This work is licensed under the terms of the GNU GPL, version 2. See
9 # the COPYING file in the top-level directory.
13 class QMPError(Exception):
16 class QMPConnectError(QMPError):
19 class QEMUMonitorProtocol:
21 self.sock.connect(self.filename)
22 data = self.__json_read()
25 if not data.has_key('QMP'):
27 return data['QMP']['capabilities']
32 def send_raw(self, line):
33 self.sock.send(str(line))
34 return self.__json_read()
36 def send(self, cmdline):
37 cmd = self.__build_cmd(cmdline)
39 resp = self.__json_read()
42 elif resp.has_key('error'):
47 def __build_cmd(self, cmdline):
48 cmdargs = cmdline.split()
49 qmpcmd = { 'execute': cmdargs[0], 'arguments': {} }
50 for arg in cmdargs[1:]:
56 qmpcmd['arguments'][opt[0]] = value
59 def __json_send(self, cmd):
60 # XXX: We have to send any additional char, otherwise
61 # the Server won't read our input
62 self.sock.send(json.dumps(cmd) + ' ')
64 def __json_read(self):
66 return json.loads(self.sock.recv(1024))
70 def __init__(self, filename):
71 self.filename = filename
72 self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)