Python memo: urlgrabber.

When I was reading test scripts of AutoQA, I saw a module which seems to be useful. That is urlgrabber. of course, Fedora provides that package.
Anyway, this module helps you to download files also it has progress meter feature. It's good feature, isn't it? I don't like waiting without any sign. so this progress meter is wonderful for me:-) Most important thing is it quite easy to use. For simple downloader, it only three lines you need to write to use download and progress meter.

This is a sample code.

#!/usr/bin/env python

import os
import urlgrabber.grabber
import urlgrabber.progress

def download_file(files):
    grabber = urlgrabber.grabber.URLGrabber()
    grabber.opts.progress_obj = urlgrabber.progress.TextMeter()

    for f in files:
        grabber.urlgrab(f)

def download_files(server, files):

    filelist = []

    for f in files:
        filelist.append(os.path.join(server, f))

    download_file(filelist)

if __name__ == "__main__":
    files = ['boot.iso', 'install.img']
    server = 'http://ftp.riken.jp/Linux/fedora/releases/13/Fedora/x86_64/os/images'

    download_files(server, files)

    print "Done."

This is an output.

[masami@moon]~% ./download.py
boot.iso                                                            | 208 MB     01:21     
install.img                                                         | 141 MB     00:53     
Done.