associação pythonbrasil[11] django zope/plone planet Início Logado como (Entrar)

Revisão 1e 2006-06-06 15:51:40

Excluir mensagem

LimitGet

Como fazer download de arquivos pela urllib limitando a banda usada.

   1 #!/usr/bin/python
   2 #
   3 # limitget.py
   4 # Copyright (c) 2005 by Nilton Volpato <first-name dot last-name @ gmail.com>
   5 #
   6 # Permission to use, copy, modify, and distribute this software and its
   7 # documentation for any purpose and without fee is hereby granted,
   8 # provided that the above copyright notice appear in all copies and that
   9 # both that copyright notice and this permission notice appear in
  10 # supporting documentation.
  11 #
  12 # This file is provided AS IS with no warranties of any kind.  The author
  13 # shall have no liability with respect to the infringement of copyrights,
  14 # trade secrets or any patents by this file or any part thereof.  In no
  15 # event will the author be liable for any lost revenue or profits or
  16 # other special, indirect and consequential damages.
  17 
  18 import urllib
  19 import time
  20 from progressbar import ProgressBar, Bar, ETA, Percentage, FileTransferSpeed
  21 
  22 def limitget(url, outfname, limit):
  23     """Downloads an url to a local file using a bandwidth limit.
  24     url: resource to download
  25     outfname: filename to which url will be copied
  26     limit: bandwidth limit in bytes/second
  27     """
  28     fp = urllib.urlopen(url)
  29     op = open(outfname, 'wb')
  30     n = 0
  31     widgets = [Percentage(), ' ', Bar(),' ', ETA(), ' ', FileTransferSpeed()]
  32     size = int(fp.headers['content-length'])
  33     pbar = ProgressBar(widgets=widgets, maxval=size).start()
  34     while 1:
  35         start = time.time()
  36         s = fp.read(limit)
  37         if not s:
  38             break
  39         op.write(s)
  40         n += len(s)
  41         timetosleep = 1.0 - (time.time() - start)
  42         if timetosleep > 0.0:
  43             time.sleep(timetosleep)
  44         pbar.update(n)
  45     pbar.finish()
  46     fp.close()
  47     op.close()
  48 
  49 if __name__ == '__main__':
  50     import sys
  51     limitget(sys.argv[1], sys.argv[2], int(sys.argv[3]))


-- NiltonVolpato