def checkServer(self): """ Attempt to call the server, and try to re-connect on error. The server could be restarted, or there could have been a network issue between the time we originally connected to the server and now, which would invalidate the original server connection. """ try: self.client.getServerInfo() except: self.client = JavaScanClient()
def __init__(self): # Connection to the scan server self.client = JavaScanClient() # Scan ID self.id = -1
class ScanClient(object): """ Base class for a scan client Can submit scans to the server and monitor them """ def __init__(self): # Connection to the scan server self.client = JavaScanClient() # Scan ID self.id = -1 def checkServer(self): """ Attempt to call the server, and try to re-connect on error. The server could be restarted, or there could have been a network issue between the time we originally connected to the server and now, which would invalidate the original server connection. """ try: self.client.getServerInfo() except: self.client = JavaScanClient() def simulate(self, commands): """ Submit a CommandSequence to the server for simulation @param commands CommandSequence or string with XML text @return Simulation info """ self.checkServer() if isinstance(commands, str): xml = commands elif isinstance(commands, CommandSequence): xml = commands.getXML() else: raise Exception('Expecting CommandSequence or XML-text') return self.client.simulateScan(xml) def submit(self, name, commands): """ Submit a CommandSequence to the server for execution @param name Name of the scan @param commands CommandSequence or string with XML text @return Scan ID """ self.checkServer() if isinstance(commands, str): xml = commands elif isinstance(commands, CommandSequence): xml = commands.getXML() else: raise Exception('Expecting CommandSequence or XML-text') self.id = self.client.submitScan(name, xml) return self.id def getScanInfo(self, id=-1): """ Get scan info @param id Scan ID, defaulting to the last submitted scan """ self.checkServer() if id == -1: id = self.id return self.client.getScanInfo(id) def printData(self, id=-1, *devices): """ Print scan data @param id: Scan ID, defaulting to the last submitted scan @param devices: One or more device names. Default: All devices in scan. """ self.checkServer() if id == -1: id = self.id data = self.client.getScanData(id) if devices: sheet = ScanDataIterator(data, devices) else: sheet = ScanDataIterator(data) sheet.printTable(System.out) def waitUntilDone(self, id=-1): """ Wait until a submitted scan has finished @param id: Scan ID, defaulting to the last submitted scan """ while True: info = self.getScanInfo(id) print info if info.getState().isDone(): break; time.sleep(1.0) def __str__(self): return "Scan client, connected to %s" % self.server.getInfo()
class ScanClient(object): """ Base class for a scan client Can submit scans to the server and monitor them """ def __init__(self): # Connection to the scan server self.client = JavaScanClient() # Scan ID self.id = -1 def checkServer(self): """ Attempt to call the server, and try to re-connect on error. The server could be restarted, or there could have been a network issue between the time we originally connected to the server and now, which would invalidate the original server connection. """ try: self.client.getServerInfo() except: self.client = JavaScanClient() def simulate(self, commands): """ Submit a CommandSequence to the server for simulation @param commands CommandSequence or string with XML text @return Simulation info """ self.checkServer() if isinstance(commands, str): xml = commands elif isinstance(commands, CommandSequence): xml = commands.getXML() else: raise Exception('Expecting CommandSequence or XML-text') return self.client.simulateScan(xml) def submit(self, name, commands): """ Submit a CommandSequence to the server for execution @param name Name of the scan @param commands CommandSequence or string with XML text @return Scan ID """ self.checkServer() if isinstance(commands, str): xml = commands elif isinstance(commands, CommandSequence): xml = commands.getXML() else: raise Exception('Expecting CommandSequence or XML-text') self.id = self.client.submitScan(name, xml) return self.id def getScanInfo(self, id=-1): """ Get scan info @param id Scan ID, defaulting to the last submitted scan """ self.checkServer() if id == -1: id = self.id return self.client.getScanInfo(id) def printData(self, id=-1, *devices): """ Print scan data @param id: Scan ID, defaulting to the last submitted scan @param devices: One or more device names. Default: All devices in scan. """ self.checkServer() if id == -1: id = self.id data = self.client.getScanData(id) if devices: sheet = ScanDataIterator(data, devices) else: sheet = ScanDataIterator(data) sheet.printTable(System.out) def waitUntilDone(self, id=-1): """ Wait until a submitted scan has finished @param id: Scan ID, defaulting to the last submitted scan """ while True: info = self.getScanInfo(id) print info if info.getState().isDone(): break time.sleep(1.0) def __str__(self): return "Scan client, connected to %s" % self.server.getInfo()