urllib's functionality with urllib2

Fuzzyman fuzzyman at gmail.com
Mon Mar 14 07:27:35 EST 2005


Monty wrote:
> Hello,
> 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
> denied...)
>

urllib2.urlopen(req)  returns a handle on the URL. This handle has
various properties - if a content length header was sent it will
include that.

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
''.join(a) afterwards
.    print 'Now transferred %s bytes.' % count

Does this help ?

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

> Thanks for any help,
> 
> Monty




More information about the Python-list mailing list