Skip to content

Instantly share code, notes, and snippets.

@brokkr

brokkr/download_file.py

Last active Aug 30, 2016
Embed
What would you like to do?
Download a file with timeouts
import urllib.request
import urllib.error
import signal
class NoMoreBufferException(Exception):
pass
class TimesUpException(Exception):
pass
def handler(signum, frame):
raise TimesUpException("Download timed out")
def download_block(u, f, block_size):
buffer = u.read(block_size)
if not buffer:
raise NoMoreBufferException("No more to download")
f.write(buffer)
def download_file(abspath, url):
'''Download function with block time outs'''
try:
u = urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
return False
except:
return False
signal.signal(signal.SIGALRM, handler)
f = open(abspath, "wb")
block_size = 8192
while True:
signal.alarm(60)
try:
download_block(u, f, block_size)
except NoMoreBufferException as e:
outcome = True
f.close()
break
except TimesUpException as e:
outcome = False
f.close()
delete_file(abspath)
break
signal.alarm(0)
signal.signal(signal.SIGALRM, signal.SIG_DFL)
return outcome
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment