当前位置: 首页>>代码示例>>Python>>正文


Python os.sendfile方法代码示例

本文整理汇总了Python中os.sendfile方法的典型用法代码示例。如果您正苦于以下问题:Python os.sendfile方法的具体用法?Python os.sendfile怎么用?Python os.sendfile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os的用法示例。


在下文中一共展示了os.sendfile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: blob_client

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def blob_client(self, csock):
        file_id = Pyro5.socketutil.receive_data(csock, 36).decode()
        print("{0} requesting file id {1}".format(csock.getpeername(), file_id))
        is_file, data = self.find_blob_data(file_id)
        if is_file:
            if hasattr(os, "sendfile"):
                print("...from file using sendfile()")
                out_fn = csock.fileno()
                in_fn = data.fileno()
                sent = 1
                offset = 0
                while sent:
                    sent = os.sendfile(out_fn, in_fn, offset, 512000)
                    offset += sent
            else:
                print("...from file using plain old read(); your os doesn't have sendfile()")
                while True:
                    chunk = data.read(512000)
                    if not chunk:
                        break
                    csock.sendall(chunk)
        else:
            print("...from memory")
            csock.sendall(data)
        csock.close() 
开发者ID:irmen,项目名称:Pyro5,代码行数:27,代码来源:server.py

示例2: _import_sendfile

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def _import_sendfile():
    # By default attempt to use os.sendfile introduced in Python 3.3:
    # http://bugs.python.org/issue10882
    # ...otherwise fallback on using third-party pysendfile module:
    # https://github.com/giampaolo/pysendfile/
    if os.name == 'posix':
        try:
            return os.sendfile  # py >= 3.3
        except AttributeError:
            try:
                import sendfile as sf
                # dirty hack to detect whether old 1.2.4 version is installed
                if hasattr(sf, 'has_sf_hdtr'):
                    raise ImportError
                return sf.sendfile
            except ImportError:
                pass 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:19,代码来源:handlers.py

示例3: _use_sendfile

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def _use_sendfile(self, producer):
        if not self.cmd_channel.use_sendfile:
            debug("starting transfer not using sendfile(2) as per server "
                  "config", self)
            return False
        if not isinstance(producer, FileProducer):
            debug("starting transfer not using sendfile(2) (directory "
                  "listing)", self)
            return False
        else:
            if not hasattr(self.file_obj, "fileno"):
                debug("starting transfer not using sendfile(2) %r has no "
                      "fileno() method" % self.file_obj, self)
                return False
        if not producer.type == 'i':
            debug("starting transfer not using sendfile(2) (text file "
                  "transfer)", self)
            return False
        debug("starting transfer using sendfile()", self)
        return True 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:22,代码来源:handlers.py

示例4: sendfile

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def sendfile(self, file, offset=0, count=None):
        """sendfile(file[, offset[, count]]) -> sent

        Send a file until EOF is reached by using high-performance
        os.sendfile() and return the total number of bytes which
        were sent.
        *file* must be a regular file object opened in binary mode.
        If os.sendfile() is not available (e.g. Windows) or file is
        not a regular file socket.send() will be used instead.
        *offset* tells from where to start reading the file.
        If specified, *count* is the total number of bytes to transmit
        as opposed to sending the file until EOF is reached.
        File position is updated on return or also in case of error in
        which case file.tell() can be used to figure out the number of
        bytes which were sent.
        The socket must be of SOCK_STREAM type.
        Non-blocking sockets are not supported.

        .. versionadded:: 1.1rc4
           Added in Python 3.5, but available under all Python 3 versions in
           gevent.
        """
        return self._sendfile_use_send(file, offset, count)

    # get/set_inheritable new in 3.4 
开发者ID:leancloud,项目名称:satori,代码行数:27,代码来源:_socket3.py

示例5: _import_sendfile

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def _import_sendfile():
    # By default attempt to use os.sendfile introduced in Python 3.3:
    # http://bugs.python.org/issue10882
    # ...otherwise fallback on using third-party pysendfile module:
    # https://github.com/giampaolo/pysendfile/
    if os.name == 'posix':
        try:
            return os.sendfile  # py >= 3.3
        except AttributeError:
            try:
                import sendfile as sf
                # dirty hack to detect whether old 1.2.4 version is installed
                if hasattr(sf, 'has_sf_hdtr'):
                    raise ImportError
                return sf.sendfile
            except ImportError:
                pass
    return None 
开发者ID:exasol,项目名称:script-languages,代码行数:20,代码来源:handlers.py

示例6: get_repr_info

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def get_repr_info(self, as_str=False, extra_info={}):
        info = OrderedDict()
        info['id'] = id(self)
        info['addr'] = "%s:%s" % (self.remote_ip, self.remote_port)
        if _is_ssl_sock(self.socket):
            info['ssl'] = True
        if self.username:
            info['user'] = self.username
        # If threads are involved sometimes "self" may be None (?!?).
        dc = getattr(self, 'data_channel', None)
        if dc is not None:
            if _is_ssl_sock(dc.socket):
                info['ssl-data'] = True
            if dc.file_obj:
                if self.data_channel.receive:
                    info['sending-file'] = dc.file_obj
                    if dc.use_sendfile():
                        info['use-sendfile(2)'] = True
                else:
                    info['receiving-file'] = dc.file_obj
                info['bytes-trans'] = dc.get_transmitted_bytes()
        info.update(extra_info)
        if as_str:
            return ', '.join(['%s=%r' % (k, v) for (k, v) in info.items()])
        return info 
开发者ID:exasol,项目名称:script-languages,代码行数:27,代码来源:handlers.py

示例7: sendfile

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def sendfile(self, file, offset=0, count=None):
        """sendfile(file[, offset[, count]]) -> sent

        Send a file until EOF is reached by using high-performance
        os.sendfile() and return the total number of bytes which
        were sent.
        *file* must be a regular file object opened in binary mode.
        If os.sendfile() is not available (e.g. Windows) or file is
        not a regular file socket.send() will be used instead.
        *offset* tells from where to start reading the file.
        If specified, *count* is the total number of bytes to transmit
        as opposed to sending the file until EOF is reached.
        File position is updated on return or also in case of error in
        which case file.tell() can be used to figure out the number of
        bytes which were sent.
        The socket must be of SOCK_STREAM type.
        Non-blocking sockets are not supported.
        """
        try:
            return self._sendfile_use_sendfile(file, offset, count)
        except _GiveupOnSendfile:
            return self._sendfile_use_send(file, offset, count) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:24,代码来源:socket.py

示例8: sendfile_wrapper

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def sendfile_wrapper(self, sock, file, offset, nbytes, headers=[], trailers=[]):
        """A higher level wrapper representing how an application is
        supposed to use sendfile().
        """
        while 1:
            try:
                if self.SUPPORT_HEADERS_TRAILERS:
                    return os.sendfile(sock, file, offset, nbytes, headers,
                                       trailers)
                else:
                    return os.sendfile(sock, file, offset, nbytes)
            except OSError as err:
                if err.errno == errno.ECONNRESET:
                    # disconnected
                    raise
                elif err.errno in (errno.EAGAIN, errno.EBUSY):
                    # we have to retry send data
                    continue
                else:
                    raise 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:22,代码来源:test_os.py

示例9: test_offset_overflow

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def test_offset_overflow(self):
        # specify an offset > file size
        offset = len(self.DATA) + 4096
        try:
            sent = os.sendfile(self.sockno, self.fileno, offset, 4096)
        except OSError as e:
            # Solaris can raise EINVAL if offset >= file length, ignore.
            if e.errno != errno.EINVAL:
                raise
        else:
            self.assertEqual(sent, 0)
        self.client.shutdown(socket.SHUT_RDWR)
        self.client.close()
        self.server.wait()
        data = self.server.handler_instance.get_data()
        self.assertEqual(data, b'') 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:18,代码来源:test_os.py

示例10: test_headers

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def test_headers(self):
        total_sent = 0
        sent = os.sendfile(self.sockno, self.fileno, 0, 4096,
                            headers=[b"x" * 512])
        total_sent += sent
        offset = 4096
        nbytes = 4096
        while 1:
            sent = self.sendfile_wrapper(self.sockno, self.fileno,
                                                    offset, nbytes)
            if sent == 0:
                break
            total_sent += sent
            offset += sent

        expected_data = b"x" * 512 + self.DATA
        self.assertEqual(total_sent, len(expected_data))
        self.client.close()
        self.server.wait()
        data = self.server.handler_instance.get_data()
        self.assertEqual(hash(data), hash(expected_data)) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:23,代码来源:test_os.py

示例11: _sock_sendfile_native

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def _sock_sendfile_native(self, sock, file, offset, count):
        try:
            os.sendfile
        except AttributeError as exc:
            raise events.SendfileNotAvailableError(
                "os.sendfile() is not available")
        try:
            fileno = file.fileno()
        except (AttributeError, io.UnsupportedOperation) as err:
            raise events.SendfileNotAvailableError("not a regular file")
        try:
            fsize = os.fstat(fileno).st_size
        except OSError as err:
            raise events.SendfileNotAvailableError("not a regular file")
        blocksize = count if count else fsize
        if not blocksize:
            return 0  # empty file

        fut = self.create_future()
        self._sock_sendfile_native_impl(fut, None, sock, fileno,
                                        offset, count, blocksize, 0)
        return await fut 
开发者ID:CedricGuillemet,项目名称:Imogen,代码行数:24,代码来源:unix_events.py

示例12: sendfile

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def sendfile(self) -> None:
        assert self.transport is not None
        out_socket = self.transport.get_extra_info('socket').dup()
        out_socket.setblocking(False)
        out_fd = out_socket.fileno()

        loop = self.loop
        data = b''.join(self._sendfile_buffer)
        try:
            await loop.sock_sendall(out_socket, data)
            if not self._do_sendfile(out_fd):
                fut = loop.create_future()
                fut.add_done_callback(partial(self._done_fut, out_fd))
                loop.add_writer(out_fd, self._sendfile_cb, fut, out_fd)
                await fut
        except asyncio.CancelledError:
            raise
        except Exception:
            server_logger.debug('Socket error')
            self.transport.close()
        finally:
            out_socket.close()

        await super().write_eof() 
开发者ID:TouwaStar,项目名称:Galaxy_Plugin_Bethesda,代码行数:26,代码来源:web_fileresponse.py

示例13: sendfile_all

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def sendfile_all(self, fileno, sockno, offset, nbytes):
        # Send file in at most 1GB blocks as some operating
        # systems can have problems with sending files in blocks
        # over 2GB.

        BLKSIZE = 0x3FFFFFFF

        if nbytes > BLKSIZE:
            for m in range(0, nbytes, BLKSIZE):
                self.sendfile_all(fileno, sockno, offset, min(nbytes, BLKSIZE))
                offset += BLKSIZE
                nbytes -= BLKSIZE
        else:
            sent = 0
            sent += sendfile(sockno, fileno, offset + sent, nbytes - sent)
            while sent != nbytes:
                sent += sendfile(sockno, fileno, offset + sent, nbytes - sent) 
开发者ID:chalasr,项目名称:Flask-P2P,代码行数:19,代码来源:wsgi.py

示例14: extract_file

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def extract_file(self, start_cluster, file_size, rel_outfile):
        abs_outfile = self.scan_environment.unpack_path(rel_outfile)
        os.makedirs(abs_outfile.parent, exist_ok=True)
        outfile = open(abs_outfile, 'wb')
        size_read = 0
        cluster_size = self.data.boot_sector.bpb.ls_per_clus * \
                self.data.boot_sector.bpb.bytes_per_ls
        for cluster in self.cluster_chain(start_cluster):
            bytes_to_read = min(cluster_size, file_size - size_read)
            start = self.offset + self.pos_data + (cluster-2) * cluster_size
            check_condition(start+bytes_to_read <= self.fileresult.filesize,
                    "file data outside file")
            os.sendfile(outfile.fileno(), self.infile.fileno(), start, bytes_to_read)
            size_read += bytes_to_read
        outfile.close()
        outlabels = []
        return (rel_outfile, outlabels) 
开发者ID:armijnhemel,项目名称:binaryanalysis-ng,代码行数:19,代码来源:UnpackParser.py

示例15: test_sendfile_fails

# 需要导入模块: import os [as 别名]
# 或者: from os import sendfile [as 别名]
def test_sendfile_fails(self):
        # Makes sure that if sendfile() fails and no bytes were
        # transmitted yet the server falls back on using plain
        # send()
        data = b'abcde12345' * 100000
        self.dummy_sendfile.write(data)
        self.dummy_sendfile.seek(0)
        self.client.storbinary('stor ' + self.tempfile, self.dummy_sendfile)
        with mock.patch('pyftpdlib.handlers.sendfile',
                        side_effect=OSError(errno.EINVAL)) as fun:
            self.client.retrbinary(
                'retr ' + self.tempfile, self.dummy_recvfile.write)
            assert fun.called
            self.dummy_recvfile.seek(0)
            datafile = self.dummy_recvfile.read()
            self.assertEqual(len(data), len(datafile))
            self.assertEqual(hash(data), hash(datafile)) 
开发者ID:giampaolo,项目名称:pyftpdlib,代码行数:19,代码来源:test_functional.py


注:本文中的os.sendfile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。