I am downloading pdfs using python requests
library by doing:
from tempfile import NamedTemporaryFile
f = NamedTemporaryFile()
response = requests.get(pdf_url)
assert response.status_code == 200 # optionally `assert response.ok`
f.write(response.content)
Every so often response.content appears to be truncated: when I do f.tell()
, I see there there are less bytes than expected. The Pdf also is broken: it does not open in a pdf reader.
When I then immediately redo the same request with the same url then the full file is downloaded, and f.tell()
shows the expected value, and the pdf opens in a pdf reader.
Is this a commonly known problem?
Note: I seem to have a memory leak - but this problem is happening when I am using 700MB and have 1300MB left.