ciao ho usato questo snippet di codice per scaricare file da un sito Web, finora i file più piccoli di 1 GB sono tutti buoni. ma ho notato un file 1.5GB è incompletarichieste response.iter_content() ottiene il file incompleto (1024 MB anziché 1,5 GB)?
# s is requests session object
r = s.get(fileUrl, headers=headers, stream=True)
start_time = time.time()
with open(local_filename, 'wb') as f:
count = 1
block_size = 512
try:
total_size = int(r.headers.get('content-length'))
print 'file total size :',total_size
except TypeError:
print 'using dummy length !!!'
total_size = 10000000
for chunk in r.iter_content(chunk_size=block_size):
if chunk: # filter out keep-alive new chunks
duration = time.time() - start_time
progress_size = int(count * block_size)
if duration == 0:
duration = 0.1
speed = int(progress_size/(1024 * duration))
percent = int(count * block_size * 100/total_size)
sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" %
(percent, progress_size/(1024 * 1024), speed, duration))
f.write(chunk)
f.flush()
count += 1
utilizzando le più recenti richieste 2.2.1 python 2.6.6, CentOS 6.4 il file di download si ferma sempre al 66,7% 1024MB, cosa mi manca? l'output:
file total size : 1581244542
...67%, 1024 MB, 5687 KB/s, 184 seconds passed
sembra che il generatore restituito da iter_content() pensa tutti i pezzi vengono recuperati e non v'è alcun errore. btw la parte dell'eccezione non è stata eseguita, perché il server ha restituito la lunghezza del contenuto nell'intestazione della risposta.
Nota "b" = bit, mentre "B" = byte (che probabilmente è ciò che intendi) –
@Jonathon ok ... orz, ho aggiornato il post – Shuman
Qual è il 's' in' s.get (...) '? –