los*_*sit 19 http http-headers
我一直试图通过使用Range标头值从特定点流式传输音频,但我总是从一开始就得到这首歌.我通过程序执行此操作,因此不确定问题是在我的代码中还是在服务器上.
如何确定服务器是否支持Range标头参数?
谢谢.
Sha*_*mer 29
该方法的HTTP规范定义它,如果服务器知道如何支持Range标题,它会的.反过来,当它向您返回内容时,它要求它返回带有标题的206 Partial Content响应代码Content-Range.否则,它将忽略Range您请求中的标头,并返回200响应代码.
这可能看起来很愚蠢,但您确定要制作有效的HTTP请求标头吗?通常,我忘记在请求中指定HTTP/1.1,或者忘记指定Range说明符,例如"bytes".
哦,如果您要做的就是检查,那么只需发送一个HEAD请求而不是GET请求.相同的标题,相同的一切,只是"HEAD"而不是"GET".如果您收到206回复,您将知道Range是否受到支持,否则您将收到200回复.
虽然我回答这个问题有点晚,但我认为我的回答将有助于未来的访客.这是一个python方法,用于检测服务器是否支持范围查询.
def accepts_byte_ranges(self, effective_url):
"""Test if the server supports multi-part file download. Method expects effective (absolute) url."""
import pycurl
import cStringIO
import re
c = pycurl.Curl()
header = cStringIO.StringIO()
# Get http header
c.setopt(c.URL, effective_url)
c.setopt(c.NOBODY, 1)
c.setopt(c.HEADERFUNCTION, header.write)
c.perform()
c.close()
header_text = header.getvalue()
header.close()
verbose_print(header_text)
# Check if server accepts byte-ranges
match = re.search('Accept-Ranges:\s+bytes', header_text)
if match:
return True
else:
# If server explicitly specifies "Accept-Ranges: none" in the header, we do not attempt partial download.
match = re.search('Accept-Ranges:\s+none', header_text)
if match:
return False
else:
c = pycurl.Curl()
# There is still hope, try a simple byte range query
c.setopt(c.RANGE, '0-0') # First byte
c.setopt(c.URL, effective_url)
c.setopt(c.NOBODY, 1)
c.perform()
http_code = c.getinfo(c.HTTP_CODE)
c.close()
if http_code == 206: # Http status code 206 means byte-ranges are accepted
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
小智 5
这是供其他人搜索如何执行此操作的。您可以使用curl:
curl -I http://exampleserver.com/example_video.mp4
Run Code Online (Sandbox Code Playgroud)
在标题中,您应该看到
Accept-Ranges: bytes
Run Code Online (Sandbox Code Playgroud)
您可以走得更远并测试取回范围
curl --header "Range: bytes=100-107" -I http://exampleserver.com/example_vide0.mp4
Run Code Online (Sandbox Code Playgroud)
在标题中,您应该看到
HTTP/1.1 206 Partial Content
Run Code Online (Sandbox Code Playgroud)
和
Content-Range: bytes 100-107/10000000
Content-Length: 8
Run Code Online (Sandbox Code Playgroud)
[您将看到文件的长度而不是10000000]
| 归档时间: |
|
| 查看次数: |
15843 次 |
| 最近记录: |