def retrieveCaptureFileForAssignedPorts(connection, sessionUrl, captureFolder): ''' This method is used to retrieve capture files from a rest session which had portCapture set to True. Args: - connection is the connection object that manages the HTTP data transfers between the client and the REST API - sessionUrl is the address of the session on which the test was ran. - captureFolder is the folder where the capture file will be saved ''' communityListUrl = "%s/ixload/test/activeTest/communityList" % sessionUrl communityList = connection.httpGet(communityListUrl) captureFolder = captureFolder.replace("\\\\", "\\") for community in communityList: portListUrl = "%s/%s/network/portList" % (communityListUrl, community.objectID) portList = connection.httpGet(portListUrl) for port in portList: captureUrl = portListUrl + "/%s/restCaptureFile" % port.objectID capturePayload = connection.httpRequest('GET', captureUrl, downloadStream=True) captureName = "Capture_%s_%s.cap" % (community.objectID, port.id) captureFile = '/'.join([captureFolder, captureName]) log("Saving capture file %s..." % (captureFile)) try: with open(captureFile, 'wb') as fileHandle: for chunk in capturePayload.iter_content(chunk_size=1024): fileHandle.write(chunk) except IOError as e: raise Exception("Error: Saving capture failed. Could not open or create file, please check path and/or permissions. Received IO error: %s" % str(e)) except Exception as e: raise Exception("Error: Saving capture failed. Received the following error:\n %s" % str(e)) else: log("Saving capture finished.")