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

Você não tem permissão para executar esta ação.

Excluir mensagem

LimitGet

Como fazer download de arquivos pela urllib limitando a banda usada. Usa a biblioteca progressbar (http://cheeseshop.python.org/pypi/progressbar).

   1 #!/usr/bin/python
   2 #
   3 # limitget.py
   4 # Copyright (c) 2006 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]))

Outro jeito de fazer é como abaixo. Mas cuidado pois esse exemplo abaixo pode não funcionar como esperado em situações particulares.

   1 #!/usr/bin/python
   2 # -*- encoding: iso-8859-1 -*-
   3 import urllib2
   4 from time import sleep
   5 
   6 import timing # To debug
   7 from sys import stdout
   8 
   9 def limit(www,bps):
  10     '''Load www address in bps B/s'''
  11     con = urllib2.urlopen(www)
  12     bytes = bps/10
  13     total_bytes = 0
  14     timing.start()
  15     while True:
  16         data = con.read(bytes)
  17         total_bytes += len(data)
  18         if not data:
  19             break
  20         stdout.write('Loaded %d bytes\r'%(total_bytes))
  21         stdout.flush()
  22         sleep(0.1)
  23         
  24     timing.finish()
  25     time_ = timing.milli()/1000.0
  26     print 'Loaded %dBytes in %0.2f seconds (%dBps)'%(total_bytes, time_, total_bytes/time_)


-- NiltonVolpato