I am looking to download a file from a http url to a local file. The file is large enough that I want to download it and save it chunks rather than read()
and write()
the whole file as a single giant string.
The interface of urllib.urlretrieve
is essentially what I want. However, I cannot see a way to set request headers when downloading via urllib.urlretrieve
, which is something I need to do.
If I use urllib2
, I can set request headers via its Request
object. However, I don't see an API in urllib2
to download a file directly to a path on disk like urlretrieve
. It seems that instead I will have to use a loop to iterate over the returned data in chunks, writing them to a file myself and checking when we are done.
What would be the best way to build a function that works like urllib.urlretrieve
but allows request headers to be passed in?