
434
|
第
17
章
➏
download_one
函数的返回值是一个
namedtuple
——
Result
,其中有个
status
字段,其
值是
HTTPStatus.not_found
或
HTTPStatus.ok
。
示例
17-13
列出的是
download_many
函数的依序下载版。代码虽然简单,不过值得分析一
下,以便后面与并发版对比。我们要关注的是报告进度、处理错误和统计下载数量的方式。
示例
17-13
flags2_sequential.py
:实现依序下载的
download_many
函数
def download_many(cc_list, base_url, verbose, max_req):
counter = collections.Counter()
➊
cc_iter = sorted(cc_list)
➋
if not verbose:
cc_iter = tqdm.tqdm(cc_iter)
➌
for cc in cc_iter:
➍
try:
res = download_one(cc, base_url, verbose)
➎
except requests.exceptions.HTTPError as exc:
➏
error_msg = 'HTTP error {res.status_code} - {res.reason}'
error_msg = error_msg.format(res=exc.response)
except requests.exceptions.ConnectionError ...