2

I've got a large file somewhere (FTP/HTTP).

I want to

  1. Download first N bytes,
  2. Check its header which is embedded into the file (whether the version differs)
  3. Then decide whether to proceed with or abort the download.

It's definitely not such a straightforward task as I've imagined (to my surprise). Even calling wget/curl externally doesn't seem to be a good solution (Maybe I overlooked the right command line option).

How could this be done as simple as possible in Python?

I'm thinking about a custom handler for ftp.retrbinary which will raise an exception as soon as the sum of blocks will be above defined value, but it's overkill in my eyes. Python code is supposed to be elegant, right?

4

1 回答 1

2

如果您只想检查标头,请发送HTTP Head而不是 GET。它将返回与 GET 相同的标头,没有消息正文。

HEAD 方法与 GET 相同,只是服务器不能在响应中返回消息体。响应 HEAD 请求的 HTTP 标头中包含的元信息应该与响应 GET 请求发送的信息相同。

HEAD 可以在这里详细发送。


编辑:

如果确实需要前 N 个字节,则可以urllib2Range 标头结合使用。 Range: bytes=0-N.

于 2013-01-31T10:41:26.610 回答