예제 #1
0
    def execute(self):
        bufferSize = getSettings().get("chunkSize")
        targetFile = self.parseTargetPath()

        try:
            f = open(targetFile, "rb")
        except FileNotFoundError:
            logger.warning("[!] FILE NOT FOUND")
            return False

        if self.communication:
            self.getConnection()().send(
                bytes(
                    json.dumps(
                        {
                            "routine": self.__class__.__name__,
                            "function": self.getMethodName()(),
                            "type": "io.stream",
                            "bytes": os.path.getsize(targetFile),
                        }
                    ),
                    "ascii",
                )
            )

        part = f.read(bufferSize)
        totalBytes = 0

        # Providing there is a chunk of data to read let us loop until fin.
        while part:
            responseSize = len(part)
            totalBytes += responseSize
            logger.info("Sending %d (%d received) bytes", responseSize, totalBytes)

            # Send off the chunk to the socket client
            if self.communication:
                self.getConnection()().send(part)

                # Read another chunk from the file
            part = f.read(bufferSize)

            # Flush and close the file stream
        f.close()

        self.setResponse(file=targetFile, bytes=totalBytes)
예제 #2
0
    def execute(self):
        bufferSize = getSettings().get('chunkSize')
        targetFile = self.parseTargetPath()

        try:
            f = open(targetFile, 'rb')
        except FileNotFoundError:
            logger.warning('[!] FILE NOT FOUND')
            return False

        if self.communication:
            self.getConnection()().send(
                bytes(
                    json.dumps({
                        "routine": self.__class__.__name__,
                        "function": self.getMethodName()(),
                        "type": "io.stream",
                        "bytes": os.path.getsize(targetFile)
                    }), 'ascii'))

        part = f.read(bufferSize)
        totalBytes = 0

        #Providing there is a chunk of data to read let us loop until fin.
        while (part):
            responseSize = len(part)
            totalBytes += responseSize
            logger.info("Sending %d (%d received) bytes", responseSize,
                        totalBytes)

            #Send off the chunk to the socket client
            if self.communication:
                self.getConnection()().send(part)

            #Read another chunk from the file
            part = f.read(bufferSize)

        #Flush and close the file stream
        f.close()

        self.setResponse(file=targetFile, bytes=totalBytes)
예제 #3
0
    def getRoutine(self, routine, function):
        importedRoutines = {}
        # Scan 'routines' defined director(y/ies)
        for routineModule in os.listdir(getSettings().get('routines').get('dir')):
            # Cherry pick .py files only for time being
            # Potentially support for routines to be created in other languages in the future.
            if routineModule.endswith('.py'):
                # Strip the file extension
                moduleName = routineModule[:-3]

                # Dynamically import the module via the file location
                spec = importlib.util.spec_from_file_location("module.name", os.path.join(getSettings().get('routines').get('dir'), "{}.py".format(moduleName)))
                importedRoutines[moduleName] = importlib.util.module_from_spec(spec)
                spec.loader.exec_module(importedRoutines[moduleName])

                # Iterate through all classes in module to see if any match the client 'routine'
                for className in importedRoutines[moduleName].classes:
                    if className == routine:
                        return getattr(importedRoutines[moduleName], routine)

        return False