Created
December 5, 2013 02:56
-
-
Save luqmaan/7799454 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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