|
|
1.1 ! root 1: # QEMU Monitor Protocol Python class ! 2: # ! 3: # Copyright (C) 2009 Red Hat Inc. ! 4: # ! 5: # Authors: ! 6: # Luiz Capitulino <[email protected]> ! 7: # ! 8: # This work is licensed under the terms of the GNU GPL, version 2. See ! 9: # the COPYING file in the top-level directory. ! 10: ! 11: import socket, json ! 12: ! 13: class QMPError(Exception): ! 14: pass ! 15: ! 16: class QMPConnectError(QMPError): ! 17: pass ! 18: ! 19: class QEMUMonitorProtocol: ! 20: def connect(self): ! 21: self.sock.connect(self.filename) ! 22: data = self.__json_read() ! 23: if data == None: ! 24: raise QMPConnectError ! 25: if not data.has_key('QMP'): ! 26: raise QMPConnectError ! 27: return data['QMP']['capabilities'] ! 28: ! 29: def close(self): ! 30: self.sock.close() ! 31: ! 32: def send_raw(self, line): ! 33: self.sock.send(str(line)) ! 34: return self.__json_read() ! 35: ! 36: def send(self, cmdline): ! 37: cmd = self.__build_cmd(cmdline) ! 38: self.__json_send(cmd) ! 39: resp = self.__json_read() ! 40: if resp == None: ! 41: return ! 42: elif resp.has_key('error'): ! 43: return resp['error'] ! 44: else: ! 45: return resp['return'] ! 46: ! 47: def __build_cmd(self, cmdline): ! 48: cmdargs = cmdline.split() ! 49: qmpcmd = { 'execute': cmdargs[0], 'arguments': {} } ! 50: for arg in cmdargs[1:]: ! 51: opt = arg.split('=') ! 52: try: ! 53: value = int(opt[1]) ! 54: except ValueError: ! 55: value = opt[1] ! 56: qmpcmd['arguments'][opt[0]] = value ! 57: return qmpcmd ! 58: ! 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) + ' ') ! 63: ! 64: def __json_read(self): ! 65: try: ! 66: return json.loads(self.sock.recv(1024)) ! 67: except ValueError: ! 68: return ! 69: ! 70: def __init__(self, filename): ! 71: self.filename = filename ! 72: self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.