How to load only the first x bytes of Python data

Situation: The file to download is a large file (> 100 MB). This takes a lot of time, especially with a slow Internet connection.

Problem: I just need a file header (the first 512 bytes), which will decide whether to download the whole file or not.

Question: Is there a way to download only the first 512 bytes of a file?

Additional info: Currently loading is done using urllib.urlretrieve in Python2.7

+4
source share
2 answers

I think that curlthey headwill work better than the Python solution:

curl https://my.website.com/file.txt | head -c 512 > header.txt

EDIT: , Python script, subprocess curl head

EDIT 2: Python: urlopen (urllib2.urlopen Python 2 urllib.request.urlopen Python 3) , , read . , urllib2.urlopen(my_url).read(512) 512 my_url

+1

URL-, , Content-Length, urllib2 Python.

def get_file_size(url):
    request = urllib2.Request(url)
    request.get_method = lambda : 'HEAD'
    response = urllib2.urlopen(request)
    length = response.headers.getheader("Content-Length")
    return int(length)

, , .

if get_file_size("http://stackoverflow.com") < 1000000:
    # Download
0

Source: https://habr.com/ru/post/1692189/


All Articles