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("ObjectID could not be found for the supplied tuple") 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) 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