urllib's functionality with urllib2
fuzzyman at gmail.com
Mon Mar 14 13:27:35 CET 2005
> Sorry for this maybe stupid newbie question but I didn't find any
> answer in all my readings about python:
> With urllib, using urlretrieve, it's possible to get the number of
> blocks transferred and the total size of the file.
> Is it possible to get those informations with urllib2 ?
> ( I have to use urllib2 and a Request Object with headers, urlretieve
> doesn't work. Well, at least, all my attempts with urllib were
urllib2.urlopen(req) returns a handle on the URL. This handle has
various properties - if a content length header was sent it will
If you read from the url in a loop you can do this :
. h = urllib2.urlopen(req)
. print h.info()
. page = ''
. count = 0
. n = 100 # number of bytes to read at a time
. while True:
. a = h.read(n)
. if not a: break
. count += len(a) # len(a) may not be same as n for final read
. page += a # prob. better to append to a list and do
. print 'Now transferred %s bytes.' % count
Does this help ?
> Thanks for any help,
More information about the Python-list