0

So I have a working downloader built using the requests module in python. The problem is it seems the server might be timing out when downloading a large file and when downloading, it just stop iterating over the content. No errors are printed as requests just thinks it is at the end of the content. Here is the code for the downloader:

def requests_download(url:str, file_name:str):
    responce = requests.get(url=url, stream=True, cookies=args['cookies'])
    with open(file_name, 'wb') as f:
        for chunk in responce.iter_content(chunk_size=1024*64):
            f.write(chunk)

I have tried adding other headers such as connection : keep-alive and have tried using my own user agent and neither seemed to help. I'd rather use stream=True as some of these files can be large and I don't want them all loaded into memory. I have only seen one other post with a similar issue they also believed that the server was timing them out. I'd just like to get confirmation at least that the problem isn't something I can fix but would need to be fixed on the servers side. Please don't recommend solutions that use other modules besides maybe urllib, though I imagine urllib would have the same problem. Sorry I don't have much of a way to reproduce the problem. Maybe slowing your connection down really slow and try downloading a relatively large video file? Also a side question but what is the recommended chunk_size that should be used for small to large files?

  • best way to check if the server is timing out is to not stream, `response = requests.get(url=url,cookies=args['cookies'])` if response is 200 you can retrieve the file from response.text, if you get an error or a 500 you will know the server timed out. This is not a practical check to your problem – Siddharth Chabra Dec 15 '21 at 07:52
  • @SiddharthChabra sadly I need the stream for downloading large files (video files for example). As far as I can tell looks like slow sever plus large files will always give this result and there isn't much I can do. Unless I want to figure out how to resume a partial download and incorporate that. :( –  Dec 15 '21 at 10:03
  • resuming is easy https://stackoverflow.com/questions/22894211/how-to-resume-file-download-in-python – Siddharth Chabra Dec 15 '21 at 12:53

0 Answers0