Requests with multiple connections

后端 未结 3 1288
猫巷女王i
猫巷女王i 2020-12-05 15:55

I use the Python Requests library to download a big file, e.g.:

r = requests.get(\"http://bigfile.com/bigfile.bin\")
content = r.content

Th

3条回答
  •  借酒劲吻你
    2020-12-05 16:32

    Here's a Python script that saves given url to a file and uses multiple threads to download it:

    #!/usr/bin/env python
    import sys
    from functools import partial
    from itertools import count, izip
    from multiprocessing.dummy import Pool # use threads
    from urllib2 import HTTPError, Request, urlopen
    
    def download_chunk(url, byterange):
        req = Request(url, headers=dict(Range='bytes=%d-%d' % byterange))
        try:
            return urlopen(req).read()
        except HTTPError as e:
            return b''  if e.code == 416 else None  # treat range error as EOF
        except EnvironmentError:
            return None
    
    def main():
        url, filename = sys.argv[1:]
        pool = Pool(4) # define number of concurrent connections
        chunksize = 1 << 16
        ranges = izip(count(0, chunksize), count(chunksize - 1, chunksize))
        with open(filename, 'wb') as file:
            for s in pool.imap(partial(download_part, url), ranges):
                if not s:
                    break # error or EOF
                file.write(s)
                if len(s) != chunksize:
                    break  # EOF (servers with no Range support end up here)
    
    if __name__ == "__main__":
        main()
    

    The end of file is detected if a server returns empty body, or 416 http code, or if the response size is not chunksize exactly.

    It supports servers that doesn't understand Range header (everything is downloaded in a single request in this case; to support large files, change download_chunk() to save to a temporary file and return the filename to be read in the main thread instead of the file content itself).

    It allows to change independently number of concurrent connections (pool size) and number of bytes requested in a single http request.

    To use multiple processes instead of threads, change the import:

    from multiprocessing.pool import Pool # use processes (other code unchanged)
    

提交回复
热议问题