[Tutor] Help: wget-- how does it work?

Kent Johnson kent37 at tds.net
Sat May 28 02:17:11 CEST 2005


Aaron Elbaz wrote:
> One of my favourite unix applications is wget.
> 
> This could be used to simulate wgets progress bar. What I'm trying to
> figure out now is how one would stat a file as it was being downloaded
> so that I can feed information to the progress bar, and what the most
> optimal way to do this is?

You can get the content-length header from the object returned from urllib2.urlopen():

  >>> import urllib2
  >>> f=urllib2.urlopen('http://www.google.com')
  >>> f
<addinfourl at 10696064 whose fp = <socket._fileobject object at 0x00A2B570>>
  >>> f.info()
<httplib.HTTPMessage instance at 0x00A331E8>
  >>> i=f.info()
  >>> i.getheader('content-length')
'1983'

Now you can read the data in chunks (using f.read(n)) and update your status bar as you go.

Kent



More information about the Tutor mailing list