import urllib class HookException Exception pass def hook chunk chunk_

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import urllib
class HookException(Exception):
pass
def hook(chunk, chunk_size, total_size):
raise HookException(total_size)
def get_size(url):
try:
urllib.urlretrieve(url, reporthook=hook)
except HookException, ex:
return ex.message
url = 'http://ya.ru'
print get_size(url)