Skip to content

Instantly share code, notes, and snippets.

@luqmaan
Created December 5, 2013 02:56
Show Gist options
  • Save luqmaan/7799454 to your computer and use it in GitHub Desktop.
Save luqmaan/7799454 to your computer and use it in GitHub Desktop.
import time
import urllib
url = 'http://pub1.di.fm/di_classictrance'
filename = 'a.mp3'
time_limit = 10
starttime = time.time()
def callback(count, blocksize, filesize):
r = urllib.urlopen(url)
print r.info()
elapsed = time.time() - starttime
if elapsed > time_limit:
raise Exception("hit timelimit")
try:
f = open(filename, 'w')
except IOError:
raise IOError('Cannot write to {0}'.format(filename))
else:
f.close()
try:
urllib.urlretrieve(url, filename=filename, reporthook=callback)
except Exception as e:
print e.message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment