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


Python FileSender.addCallback方法代码示例

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


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

示例1: render_GET

# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addCallback [as 别名]
    def render_GET(self, request):
        # no auth here on purpose, to allow anyone to view, even across home
        # servers.

        # TODO: A little crude here, we could do this better.
        filename = request.path.split('/')[-1]
        # be paranoid
        filename = re.sub("[^0-9A-z.-_]", "", filename)

        file_path = self.directory + "/" + filename

        logger.debug("Searching for %s", file_path)

        if os.path.isfile(file_path):
            # filename has the content type
            base64_contentype = filename.split(".")[1]
            content_type = base64.urlsafe_b64decode(base64_contentype)
            logger.info("Sending file %s", file_path)
            f = open(file_path, 'rb')
            request.setHeader('Content-Type', content_type)
            d = FileSender().beginFileTransfer(f, request)

            # after the file has been sent, clean up and finish the request
            def cbFinished(ignored):
                f.close()
                request.finish()
            d.addCallback(cbFinished)
        else:
            respond_with_json_bytes(
                request,
                404,
                json.dumps(cs_error("Not found", code=Codes.NOT_FOUND)),
                send_cors=True)

        return server.NOT_DONE_YET
开发者ID:winsontan520,项目名称:synapse,代码行数:37,代码来源:content_repository.py

示例2: test_pullFileConsumer

# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addCallback [as 别名]
 def test_pullFileConsumer(self):
     fileToSend = StringIO(fileData)
     clock = Clock()
     consumer = FileConsumer(clock)
     d = FileSender().beginFileTransfer(fileToSend, consumer)
     finished = []
     d.addCallback(finished.append)
     while not finished:
         clock.advance(1)
     self.assertEqual(consumer.value(), fileData)
开发者ID:habnabit,项目名称:txampproducer,代码行数:12,代码来源:test_integration.py

示例3: send_open_file

# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addCallback [as 别名]
def send_open_file(openFile, request):
    '''Use FileSender to asynchronously send an open file

    [JBY] From: http://stackoverflow.com/questions/1538617/http-download-very-big-file'''
    dd = FileSender().beginFileTransfer(openFile, request)

    def cbFinished(ignored):
        openFile.close()
        request.finish()
    
    dd.addCallback(cbFinished).addErrback(err)
开发者ID:guoyr,项目名称:geo-caching,代码行数:13,代码来源:image_transfer_handler.py

示例4: download_file

# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addCallback [as 别名]
def download_file(request, name, path, file_is_temp=False):
    file = open(path, 'r')

    def finalize(*args, **kwargs):
        request.finish()
        file.close()
        if file_is_temp:
            os.remove(path)

    request.setHeader('Content-Disposition', 'attachment; filename="{}"'.format(name))

    file_sender = FileSender().beginFileTransfer(file, request)
    file_sender.addCallback(finalize)

    return NOT_DONE_YET
开发者ID:aseroglazov,项目名称:twisted_test,代码行数:17,代码来源:tools.py

示例5: sendOpenFile

# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addCallback [as 别名]
def sendOpenFile(request, openFile):
    '''Use FileSender to asynchronously send an open file

    [JBY] From: http://stackoverflow.com/questions/1538617/http-download-very-big-file'''

    contentType, junk = mimetypes.guess_type(request.path)
    request.setHeader('Content-Type', contentType if contentType else 'text/plain')
    dd = FileSender().beginFileTransfer(openFile, request)

    def cbFinished(ignored):
        openFile.close()
        request.finish()
    
    dd.addErrback(err)
    dd.addCallback(cbFinished)
    return server.NOT_DONE_YET
开发者ID:huangdehui2013,项目名称:simple-twisted-image-server,代码行数:18,代码来源:server.py

示例6: render_GET

# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addCallback [as 别名]
    def render_GET(self, request):
        # no auth here on purpose, to allow anyone to view, even across home
        # servers.

        # TODO: A little crude here, we could do this better.
        filename = request.path.split('/')[-1]
        # be paranoid
        filename = re.sub("[^0-9A-z.-_]", "", filename)

        file_path = self.directory + "/" + filename

        logger.debug("Searching for %s", file_path)

        if os.path.isfile(file_path):
            # filename has the content type
            base64_contentype = filename.split(".")[1]
            content_type = base64.urlsafe_b64decode(base64_contentype)
            logger.info("Sending file %s", file_path)
            f = open(file_path, 'rb')
            request.setHeader('Content-Type', content_type)

            # cache for at least a day.
            # XXX: we might want to turn this off for data we don't want to
            # recommend caching as it's sensitive or private - or at least
            # select private. don't bother setting Expires as all our matrix
            # clients are smart enough to be happy with Cache-Control (right?)
            request.setHeader(
                "Cache-Control", "public,max-age=86400,s-maxage=86400"
            )

            d = FileSender().beginFileTransfer(f, request)

            # after the file has been sent, clean up and finish the request
            def cbFinished(ignored):
                f.close()
                finish_request(request)
            d.addCallback(cbFinished)
        else:
            respond_with_json_bytes(
                request,
                404,
                json.dumps(cs_error("Not found", code=Codes.NOT_FOUND)),
                send_cors=True)

        return server.NOT_DONE_YET
开发者ID:0-T-0,项目名称:synapse,代码行数:47,代码来源:content_repository.py

示例7: get

# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addCallback [as 别名]
    def get(self, **kwargs):
        """
        Get the contents of the specified task log.  The log will be
        returned in CSV format with the following fields:

            * ISO8601 timestamp
            * Stream number (0 == stdout, 1 == stderr)
            * Line number
            * Parent process ID
            * Message the process produced

        The log file identifier is configurable and relies on the
        `jobtype_task_log_filename` configuration option.  See the
        configuration documentation for more information about the
        default value.

        .. http:get:: /api/v1/tasklogs/<identifier> HTTP/1.1

            **Request**

            .. sourcecode:: http

                GET /api/v1/tasklogs/<identifier> HTTP/1.1

            **Response**

            .. sourcecode:: http

                HTTP/1.1 200 OK
                Content-Type: text/csv

                2015-05-07T23:42:53.730975,0,15,42,Hello world

        :statuscode 200: The log file was found, it's content will be returned.
        """
        request = kwargs["request"]

        if request_from_master(request):
            config.master_contacted()

        if len(request.postpath) != 1:
            return (
                dumps({"error": "Did not specify a log identifier"}),
                BAD_REQUEST,
                {"Content-Type": ["application/json"]}
            )

        log_identifier = request.postpath[0]
        if "/" in log_identifier or "\\" in log_identifier:
            return (
                dumps({"error": "log_identifier must not contain "
                                "directory separators"}),
                BAD_REQUEST,
                {"Content-Type": ["application/json"]}
            )

        path = join(config["jobtype_task_logs"], log_identifier)

        try:
            logfile = self._open_file(path, "rb")
        except Exception as error:
            request.responseHeaders.setRawHeaders(
                "Content-Type", ["application/json"])

            if getattr(error, "errno", None) == ENOENT:
                return dumps({"error": "%s does not exist" % path}), NOT_FOUND

            logger.error("GET %s failed: %s", request.postpath, error)
            return dumps({"error": str(error)}), INTERNAL_SERVER_ERROR

        # TODO: deferToThread for open? (and possibly send)
        request.setResponseCode(OK)
        request.responseHeaders.setRawHeaders("Content-Type", ["text/csv"])
        deferred = FileSender().beginFileTransfer(logfile, request)
        deferred.addCallback(lambda *_: request.finish())
        deferred.addCallback(lambda *_: logfile.close())

        return NOT_DONE_YET
开发者ID:xlhtc007,项目名称:pyfarm-agent,代码行数:80,代码来源:tasklogs.py


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