I have following small piece of python script:
from urllib.request import urlopen
def download_file(url):
fp = open(url.split("=")[-1] + ".pdf", 'wb')
req = urlopen(url)
CHUNK = 20480
chunk = req.read(CHUNK)
fp.write(chunk)
fp.close()
for i in range(1, 10000, 1):
download_file("__some__url__" + str(i))
print("Done.")
I keep this script running but after sometime(let's say after downloading 100 files) due to some reason it gives an error: urllib.error.URLError: <urlopen error [WinError 10054] An existing connection was forcibly closed by the remote host>
How can I modify my code to handle that error i.e it shouldn't stop script and handle i.e wait for connection to restore and then resume downloading where it left?
PS: I know it only downloads 20KB from URL.