Python seek on remote file using HTTP

前端 未结 4 1682
遥遥无期
遥遥无期 2020-12-05 08:41

How do I seek to a particular position on a remote (HTTP) file so I can download only that part?

Lets say the bytes on a remote file were: 1234567890

I wanna

4条回答
  •  鱼传尺愫
    2020-12-05 08:58

    I highly recommend using the requests library. It is easily the best HTTP library I have ever used. In particular, to accomplish what you have described, you would do something like:

    import requests
    
    url = "http://www.sffaudio.com/podcasts/ShellGameByPhilipK.Dick.pdf"
    
    # Retrieve bytes between offsets 3 and 5 (inclusive).
    r = requests.get(url, headers={"range": "bytes=3-5"})
    
    # If a 4XX client error or a 5XX server error is encountered, we raise it.
    r.raise_for_status()
    

提交回复
热议问题