How to perform time limited response download with python requests?

前端 未结 3 1858
灰色年华
灰色年华 2020-12-29 10:34

When downloading a large file with python, I want to put a time limit not only for the connection process, but also for the download.

I am trying with the following

3条回答
  •  鱼传尺愫
    2020-12-29 10:53

    When using Requests' prefetch=False parameter, you get to pull in arbitrary-sized chunks of the respone at a time (rather than all at once).

    What you'll need to do is tell Requests not to preload the entire request and keep your own time of how much you've spent reading so far, while fetching small chunks at a time. You can fetch a chunk using r.raw.read(CHUNK_SIZE). Overall, the code will look something like this:

    import requests
    import time
    
    CHUNK_SIZE = 2**12  # Bytes
    TIME_EXPIRE = time.time() + 5  # Seconds
    
    r = requests.get('http://ipv4.download.thinkbroadband.com/1GB.zip', prefetch=False)
    
    data = ''
    buffer = r.raw.read(CHUNK_SIZE)
    while buffer:
        data += buffer
        buffer = r.raw.read(CHUNK_SIZE)
    
        if TIME_EXPIRE < time.time():
            # Quit after 5 seconds.
            data += buffer
            break
    
    r.raw.release_conn()
    
    print "Read %s bytes out of %s expected." % (len(data), r.headers['content-length'])
    

    Note that this might sometimes use a bit more than the 5 seconds allotted as the final r.raw.read(...) could lag an arbitrary amount of time. But at least it doesn't depend on multithreading or socket timeouts.

提交回复
热议问题