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 ''' communtiyListUrl = "%s/ixload/test/activeTest/communityList" % sessionUrl communityList = connection.httpGet(communtiyListUrl) captureFolder = captureFolder.replace("\\\\", "\\") for community in communityList: portListUrl = "%s/%s/network/portList" % (communtiyListUrl, 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]) fileHandle = None try: with open(captureFile, 'wb') as fileHandle: for chunk in capturePayload.iter_content(chunk_size=1024): fileHandle.write(chunk) except IOError: log("Could not open or create file, please check path and/or permissions") return 2 finally: if fileHandle: fileHandle.close() return 0