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


Python Response.headers["X-Expected-Content-Length"]方法代码示例

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


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

示例1: file

# 需要导入模块: from pyramid.response import Response [as 别名]
# 或者: from pyramid.response.Response import headers["X-Expected-Content-Length"] [as 别名]
def file(request):
    """
    HTTP standard does not allow us to respond with 206/Partial Content with unknown range length,
    so we'll just implement download resuming our own way.

    When necessary, getting content_offset from Range request header can be done like this:

    content_offset = None
    if request.range is not None:
        if request.range.start > 0 and request.range.end is None:
            content_offset = request.range.start

    And responding can be done like this:

    content_length = NotImplemented
    if content_offset is not None:
        response.content_length = content_length - content_offset + 1
        response.content_range = ContentRange(content_offset, content_length, content_length)
        response.status_code = 206
    """

    path = file_path_for_serving(request)

    if file_can_be_transfered_directly(path):
        f = open(path)
        expected_length = os.path.getsize(path)
    else:
        f = IncompleteFile(convert_file_path(request))

        d = os.path.dirname(f.path)
        if not os.path.exists(d):
            os.makedirs(d)

        stdout, stderr = subprocess.Popen(["/usr/bin/avconv", "-i", path], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
        m_duration = re.search("Duration: ([0-9:.]+),", stderr)
        if m_duration:
            [h, m, s] = m_duration.group(1).split(":")
            [s, ms] = s.split(".")
            expected_length = ((int(h) * 3600 + int(m) * 60 + int(s)) * 1000 + int(ms) * 10) * (BITRATE * 1024) / 8 / 1000
        else:
            expected_length = None

        if not os.path.exists(f.path):
            def avconv_thread():
                while True:
                    code = subprocess.call([
                        "/usr/bin/avconv", "-i", path,
                        "-acodec", "libmp3lame", "-ab", "%dk" % BITRATE, "-ar", "44100", "-f", "mp3",            
                        "-y", f.path
                    ])
                    if code == 0:
                        break
                f.set_completed()
            threading.Thread(target=avconv_thread).start()

    content_offset = int(request.GET.get("content_offset", 0))
    if content_offset > 0:
        f.read(content_offset)

    def app_iter():
        for data in iter(lambda: f.read(8192), ""):
            yield data
        f.close()

    response = Response()
    response.app_iter = app_iter()
    if expected_length:
        response.headers["X-Expected-Content-Length"] = str(expected_length)
    return response
开发者ID:Gordon01,项目名称:player.thelogin.ru,代码行数:71,代码来源:views.py


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