Fetch first n bytes from the URL
Asked Answered
P

1

5

Is that possible to fetch only a number of bytes from some URL and then close the connection with urllib/urllib2? Or even may be a part from n-th byte to k-th? There is a page on that side and I don't need to load the whole page, only a piece of it.

Pageantry answered 30/10, 2011 at 11:47 Comment(0)
E
7

You can set the Range header to request a certain range of bytes, but you are dependent on the server to honor the request:

import urllib2
req = urllib2.Request('http://www.python.org/')
#
# Here we request that bytes 18000--19000 be downloaded.
# The range is inclusive, and starts at 0.
#
req.headers['Range']='bytes=%s-%s' % (18000, 19000)
f = urllib2.urlopen(req)
# This shows you the actual bytes that have been downloaded.
content_range=f.headers.get('Content-Range')
print(content_range)
# bytes 18000-18030/18031
Excommunicative answered 30/10, 2011 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.