def retrieveCaptureFileForPorts(connection, sessionUrl, communityPortIdTuple, captureFile): ''' 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 - communityPortIdTuple is a tuple composed of (communityID and portName) - communityID is the id of the community list for which captures should be retrieved. - portName is the name of the port for which ('1.5.1', not 'Port 1.5.1') - sessionUrl is the address of the session on which the test was ran. - captureFile is the save path for the capture file Error Codes: - 0 No error - 1 Invalid portId - 2 Cannot create/open captureFile ''' communityObjectID, portID = communityPortIdTuple portObjectID = getPortObjectId(connection, sessionUrl, communityPortIdTuple) if portObjectID is None: log("Error: Port objectID could not be found for port with ID: %s" % (portID)) return 1 captureFile = captureFile.replace("\\\\", "\\") portUrl = sessionUrl + ("/ixload/test/activeTest/communityList/%s/network/portList" % communityObjectID) captureUrl = portUrl + "/%s/restCaptureFile" % portObjectID capturePayload = connection.httpRequest('GET', captureUrl, downloadStream=True) 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: log("Error: Saving capture failed. Could not open or create file, please check path and/or permissions. Received IO error: %s" % (str(e))) return 2 except Exception as e: log("Error: Saving capture failed. Received the following error:\n %s" % (str(e))) return 2 else: log("Saving capture finished.") return 0