I'm using the Python requests library to open URLs. The URL is to a word document. Manually on a browser, I can access the URL, which automatically leads to a download of the document. I am able to successuflly download the document.
However, using requests, I'm getting a ChunkedEncodingError.
My code:
import requests
url = 'https://legalref.judiciary.hk/doc/judg/word/vetted/other/ch/2008/CACC000213A_2008.doc'
res = requests.get(url)
print(res)
The error:
raise ChunkedEncodingError(e) requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(16834 bytes read, 87102 more expected)', IncompleteRead(16834 bytes read, 87102 more expected))
I've also tried using other libraries, like aiohttp and urllib3, but errors also arise.
Retrying the request doesn't work, as I am getting an error each time.
If someone could help, that would be great! Some other posts say it could be a server side problem. But it works fine on a browser, and more technical details are beyond me.
This certainly is a server-side problem – it occurs even when using
wget, thoughwget(and your browser) is smart enough to retry from the failing byte:You can implement similar logic by using
requests.get(..., stream=True), looking at theContent-Lengthyou get, and comparing it to the bytes you've managed to write; if you get an exception and you've read less than you're expecting (viaContent-Length), try again with aRange: bytes={start_byte}-style header: