python-requests - requests.get 大文件成功,但文件大小随机小于实际大小
问题描述
我的代码:
authentication = (myUsername, myPassword)
r = requests.get(source_url, stream=True, auth=authentication)
if r.status_code == 200:
with open(target_file, 'wb') as f:
right_size = int(r.headers['Content-Length'])
for chunk in r.iter_content(chunk_size=512 * 1024):
if chunk:
f.write(chunk)
file_size = f.tell()
if right_size > file_size:
print("File {0} downloading issue: right size: {1}, end size: {2} ".format(filename, right_size, file_size))
else:
print("Succeeded")
...
当我用一个大的 jar 文件进行测试时,它报告了
File XXX.jar downloading problem: right size: 112835860, end size: 6564711
此问题随机出现,因此,它不是网络服务器配置问题。
我尝试使用 shutil.copyfileobj(full_url, filename, length=512 * 1024) 来获取文件,但仍然随机遇到相同的问题。
有什么建议吗?
解决方案
推荐阅读
- python - XGBoost 自定义目标函数 - 平方归一化误差
- mysql - docker:来自守护进程的错误响应:端口不可用:监听 tcp 0.0.0.0:3306:绑定:地址已在使用中
- c# - 如何从 Datarow.ItemArray 中删除一些列
- javascript - 如何使用反应在列表中应用过滤器
- php - 如何在 PHP 中使用 strtok?
- java - 随机平均数在 Java 中不起作用
- java - 在 Scene Builder 中加载 Jar 文件的问题
- python - 对每一行应用一个函数,其中函数使用 DataFrame 的所有先前行
- windows - Git 在 Windows 上检查具有 LF 行结尾的特定文件类型
- angular - 使用 Angular 自定义构建器进行资产修订