How to use the fbtftp.base_handler.ResponseData function in fbtftp

To help you get started, we’ve selected a few fbtftp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebook / fbtftp / examples / server.py View on Github external
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.

# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import argparse
import logging
import os

from fbtftp.base_handler import BaseHandler
from fbtftp.base_handler import ResponseData
from fbtftp.base_server import BaseServer


class FileResponseData(ResponseData):
    def __init__(self, path):
        self._size = os.stat(path).st_size
        self._reader = open(path, "rb")

    def read(self, n):
        return self._reader.read(n)

    def size(self):
        return self._size

    def close(self):
        self._reader.close()


def print_session_stats(stats):
    logging.info("Stats: for %r requesting %r" % (stats.peer, stats.file_path))
github facebook / fbtftp / fbtftp / base_handler.py View on Github external
class ResponseData:
    """A base class representing a file-like object"""

    def read(self, n):
        raise NotImplementedError()

    def size(self):
        raise NotImplementedError()

    def close(self):
        raise NotImplementedError()


class StringResponseData(ResponseData):
    """
    A convenience subclass of `ResponseData` that transforms an input String
    into a file-like object.
    """

    def __init__(self, string):
        self._size = len(string.encode("latin-1"))
        self._reader = io.StringIO(string)

    def read(self, n):
        return bytes(self._reader.read(n).encode("latin-1"))

    def size(self):
        return self._size

    def close(self):