python urllib urlretrieve学习使用
程序员文章站
2022-05-07 19:30:26
import urllib
import os
def reporthook(blocks_read, block_size, total_size):
if not b...
import urllib import os def reporthook(blocks_read, block_size, total_size): if not blocks_read: print "connection opened" return if total_size
输出如下:
connection opened read 1 blocks, 8192/55662, 15% read 2 blocks, 16384/55662, 29% read 3 blocks, 24576/55662, 44% read 4 blocks, 32768/55662, 59% read 5 blocks, 40960/55662, 74% read 6 blocks, 49152/55662, 88% read 7 blocks, 57344/55662, 103% file: /tmp/tmp4lhkga headers: date: wed, 19 feb 2014 08:55:20 gmt content-type: text/html; charset=utf-8 cache-control: max-age=1800 x-cacheable: mi-www-cacheable server: mife/3.0 expires: wed, 19 feb 2014 09:25:20 gmt powered-by-chinacache: miss from 010519h3sp.4 age: 990 content-length: 55662 powered-by-chinacache: hit from 01005143sg connection: close file exists before cleanup: true file still exists: false
可以看到最后竟然读取了103%, 这是由于每次读取都是按照block 8192bytes来读取的,就算最后不满8192bytes, 也返回一个block的size