#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
import requests
url = "http://web.debug.only.bfw.wiki/asset/logo.png"
filename = "largefile.zip"
# 开始下载文件
response = requests.get(url, stream=True)
total_size = int(response.headers.get('Content-Length', 0))
block_size = 1024*1024 # 每次写入文件的大小
written_size = 0
with open(filename, "wb") as out_file:
for buffer in response.iter_content(block_size):
if not buffer:
break
out_file.write(buffer)
written_size += len(buffer)
# 显示下载进度
percent = written_size * 100 / total_size
print(f"Downloaded {written_size}/{total_size} bytes ({percent:.2f}%)")
print("Download complete.")
网友回复