paraproxio.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:paraproxio 作者: intagger 项目源码 文件源码
def process_parallel(self, message: RawRequestMessage, payload) -> aiohttp.Response:
        """Try process a request parallel. Returns True in case of processed parallel, otherwise False."""
        # Checking the opportunity of parallel downloading.
        if message.method != hdrs.METH_GET or not need_file_to_parallel(message.path):
            return None
        head = await self.get_file_head(message.path)
        if head is None:
            return None
        accept_ranges = head.get(hdrs.ACCEPT_RANGES)
        if not accept_ranges or accept_ranges.lower() != 'bytes':
            return None
        content_length = head.get(hdrs.CONTENT_LENGTH)
        if content_length is None:
            return None
        content_length = int(content_length)
        if content_length <= 0 or content_length < DEFAULT_PART_SIZE:
            return None

        # All checks pass, start a parallel downloading.
        self.log_debug("PARALLEL GET {!r} [{!s} bytes].".format(message.path, content_length))

        # Get additional file info.
        content_type = head.get(hdrs.CONTENT_TYPE)

        # Prepare a response to a client.
        client_res = aiohttp.Response(self.writer, 200, http_version=message.version)
        client_res.add_header(hdrs.CONTENT_LENGTH, str(content_length))
        if content_type:
            client_res.add_header(hdrs.CONTENT_TYPE, content_type)
        client_res.add_header(PARALLELS_HEADER, str(self._parallels))
        client_res.send_headers()

        try:
            await self._cached_downloader.download(message.path, head, lambda chunk: client_res.write(chunk))
            client_res.write_eof()
        except Exception as exc:
            self.log_debug("CANCELLED PARALLEL GET {!r}. Caused by exception: {!r}.".format(message.path, exc))
            raise

        return client_res
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号