python request下载文件时,显示进度以及网速
import requests
import time def downloadFile(name, url):
headers = {'Proxy-Connection':'keep-alive'}
r = requests.get(url, stream=True, headers=headers)
length = float(r.headers['content-length'])
f = open(name, 'wb')
count =
count_tmp =
time1 = time.time()
for chunk in r.iter_content(chunk_size = ):
if chunk:
f.write(chunk)
count += len(chunk)
if time.time() - time1 > :
p = count / length *
speed = (count - count_tmp) / / /
count_tmp = count
print(name + ': ' + formatFloat(p) + '%' + ' Speed: ' + formatFloat(speed) + 'M/S')
time1 = time.time()
f.close() def formatFloat(num):
return '{:.2f}'.format(num) if __name__ == '__main__':
downloadFile('360.exe', 'http://down.360safe.com/setup.exe')
输出
bogon:Desktop macname$ python3 test.py
.exe: 4.82% Speed: 1.98M/S
.exe: 9.36% Speed: 1.86M/S
.exe: 13.79% Speed: 1.81M/S
.exe: 18.15% Speed: 1.79M/S
.exe: 24.17% Speed: 2.47M/S
.exe: 29.52% Speed: 2.19M/S
.exe: 35.41% Speed: 2.41M/S
.exe: 41.31% Speed: 2.42M/S
.exe: 46.90% Speed: 2.29M/S
.exe: 52.36% Speed: 2.24M/S
.exe: 58.64% Speed: 2.57M/S
.exe: 64.07% Speed: 2.23M/S
.exe: 69.33% Speed: 2.16M/S
.exe: 73.85% Speed: 1.85M/S
.exe: 79.36% Speed: 2.26M/S
.exe: 85.48% Speed: 2.51M/S
.exe: 92.27% Speed: 2.78M/S
.exe: 97.85% Speed: 2.28M/S
python request下载文件时,显示进度以及网速的更多相关文章
- 实现在 .net 中使用 HttpClient 下载文件时显示进度
在 .net framework 中,要实现下载文件并显示进度的话,最简单的做法是使用 WebClient 类.订阅 DownloadProgressChanged 事件就行了. 但是很可惜,WebC ...
- 使用libcurl开源库和Duilib做的下载文件并显示进度条的小工具
转载:http://blog.csdn.net/mfcing/article/details/43603525 转载:http://blog.csdn.net/infoworld/article/de ...
- C# Winform下载文件并显示进度条
private void btnDown_Click(object sender, EventArgs e) { DownloadFile("http://localhost:1928/We ...
- Winform下载文件并显示进度条
本来是要研究怎样判断下载完成,结果找到这个方法,可以在这个方法完成之后提示下载完成. 代码如下: using System; using System.Collections.Generic; usi ...
- [c#]WebClient异步下载文件并显示进度
摘要 在项目开发中经常会用到下载文件,这里使用winform实现了一个带进度条的例子. 一个例子 using System; using System.Collections.Generic; usi ...
- 通过HttpUrlConnection下载文件并显示进度条
实现效果: 核心下载块: int count = 0; URL url = new URL("http://hezuo.downxunlei.com/xunlei_hezuo/thunder ...
- requests模块下载视频 显示进度和网速
requests 下载视频 import os,time import requests def downloadFile(name, url): headers = {'Proxy-Connecti ...
- AsyncTask用法解析-下载文件动态更新进度条
1. 泛型 AysncTask<Params, Progress, Result> Params:启动任务时传入的参数,通过调用asyncTask.execute(param)方法传入. ...
- 下载文件时-修改文件名字 Redis在Windows中安装方法 SVN安装和使用(简单版) WinForm-SQL查询避免UI卡死 Asp.Net MVC Https设置
下载文件时-修改文件名字 1后台代码 /// <summary> /// 文件下载2 /// </summary> /// <param name="Fil ...
随机推荐
- [Xcode 实际操作]四、常用控件-(3)UILabel文本标签的使用
目录:[Swift]Xcode实际操作 本文将演示标签控件的基础用法, 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class Vie ...
- 历年NOIP真题总结
前言:最近把历年的NOIP真题肝了一遍(还有3个紫题先咕掉了),主要是到1998年的提高组的题.把题目的做题简要思路搁在这儿,一个是为了考前翻一翻,想想自己的哪些思路要梳理的什么什么的,反正怎么说呢, ...
- Chinese Zodiac (水题)
The Chinese Zodiac, known as Sheng Xiao, is based on a twelve-year cycle, each year in the cycle rel ...
- web.xml中如何设置配置文件的加载路径
web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志.数据库.spring的文件等都在此时被加载,下面是两种常用的配置文件加载路径,即配置文件 ...
- 【ACM】吝啬的国度 - DFS (图)
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...
- jquery——属性操作、特殊效果
1. attr().prop() 取出或者设置某个属性的值 <!DOCTYPE html> <html lang="en"> <head> &l ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- C. Jury Marks 思维题
http://codeforces.com/contest/831/problem/C 做的时候想不到,来了个暴力. 对于每个b[i],枚举每一个a[i],就有1个可能的情况. 然后用vector存起 ...
- Sqoop Export HDFS
Sqoop Export应用场景——直接导出 直接导出 我们先复制一个表,然后将上一篇博文(Sqoop Import HDFS)导入的数据再导出到我们所复制的表里. sqoop export \ -- ...
- Windows下使用nvm管理多个Node.js 版本
下载 https://github.com/coreybutler/nvm-windows/releases 安装 配置 使用 # nvm install 版本号 # nvm list ...