从服务器读取文件

2024-04-19 20:10:52 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从服务器读取文件,并从偏移量开始(类似于wget-c的行为)?我必须向服务器发送哪些邮件头?服务器必须支持什么样的未来?在


Tags: 文件服务器邮件wget偏移量服务器发送
2条回答

您应该在请求中使用Range头。但是只有当服务器通过Accept-Ranges响应头通知您它接受范围请求时,才可以使用它。在

这是一个示例会话。假设我们有兴趣得到this picture的一部分。首先,我们发送一个HTTPHEAD请求来确定:a)如果服务器支持字节范围,b)内容长度:

> HEAD /2238/2758537173_670161cac7_b.jpg HTTP/1.1
> Host: farm3.static.flickr.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Thu, 08 Jul 2010 12:22:12 GMT
< Content-Type: image/jpeg
< Connection: keep-alive
< Server: Apache/2.0.52 (Red Hat)
< Expires: Mon, 28 Jul 2014 23:30:00 GMT
< Last-Modified: Wed, 13 Aug 2008 06:13:54 GMT
< Accept-Ranges: bytes
< Content-Length: 350015

接下来,我们发送一个GET请求,该请求带有Range报头,请求图片的前11个字节:

^{pr2}$

这是前11个字节的十六进制转储:

00000000  ff d8 ff e0 00 10 4a 46  49 46 00                 |......JFIF.|
0000000b

有关更多信息,请参见HTTP rfc2616中的Range header specification。在

http://www.gnu.org/software/wget/manual/wget.html

Note that ‘-c’ only works with ftp servers and with http servers that support the Range header.

http://tools.ietf.org/html/rfc2616

Examples of byte-ranges-specifier values (assuming an entity-body of
length 10000):

  - The first 500 bytes (byte offsets 0-499, inclusive):  bytes=0-
    499

  - The second 500 bytes (byte offsets 500-999, inclusive):
    bytes=500-999

  - The final 500 bytes (byte offsets 9500-9999, inclusive):
    bytes=-500

  - Or bytes=9500-

  - The first and last bytes only (bytes 0 and 9999):  bytes=0-0,-1

  - Several legal but not canonical specifications of the second

500 bytes (byte offsets 500-999, inclusive): bytes=500-600,601-999 bytes=500-700,601-999

所以你应该

Range:bytes=9500-

To test if a server support it you can test the accept-range as such

Origin servers that accept byte-range requests MAY send

Accept-Ranges: bytes

but are not required to do so. Clients MAY generate byte-range requests without having received this header for the resource involved. Range units are defined in section 3.12.

Servers that do not accept any kind of range request for a resource MAY send

Accept-Ranges: none

to advise the client not to attempt a range request.

相关问题 更多 >