Python获取下载速度并显示进度条,,#!/usr/bin
Python获取下载速度并显示进度条,,#!/usr/bin
#!/usr/bin/python3# -*- coding:utf-8 -*-import sysimport timefrom urllib import request‘‘‘ urllib.urlretrieve 的回调函数:def callbackfunc(blocknum, blocksize, totalsize): @blocknum: 已经下载的数据块 @blocksize: 数据块的大小 @totalsize: 远程文件的大小‘‘‘def Schedule(blocknum, blocksize, totalsize): speed = (blocknum * blocksize) / (time.time() - start_time) # speed_str = " Speed: %.2f" % speed speed_str = " Speed: %s" % format_size(speed) recv_size = blocknum * blocksize # 设置下载进度条 f = sys.stdout pervent = recv_size / totalsize percent_str = "%.2f%%" % (pervent * 100) n = round(pervent * 50) s = (‘#‘ * n).ljust(50, ‘-‘) f.write(percent_str.ljust(8, ‘ ‘) + ‘[‘ + s + ‘]‘ + speed_str) f.flush() # time.sleep(0.1) f.write(‘\r‘)# 字节bytes转化K\M\Gdef format_size(bytes): try: bytes = float(bytes) kb = bytes / 1024 except: print("传入的字节格式不对") return "Error" if kb >= 1024: M = kb / 1024 if M >= 1024: G = M / 1024 return "%.3fG" % (G) else: return "%.3fM" % (M) else: return "%.3fK" % (kb)if __name__ == ‘__main__‘: # print(format_size(1222222222)) start_time = time.time() filename = ‘test.data‘ url = ‘http://ip:port/path/speed.test‘ request.urlretrieve(url, filename, Schedule)
Python获取下载速度并显示进度条
相关内容
- python二维图像输出操作大全(非常全)!,,//2019.07.
- [Python 多线程] 详解daemon属性值None,False,True的区别 (五
- python 非root 用户安装第三方模块,,利用非root用户安
- python 使用yield进行数据的流式处理,,demo:从文件中取
- Python eval,exac,compile,,# eval 是把字
- Python3基础 一个类中 属性与方法重名时,属性会覆盖方
- Python如何让读取文件内容为字符串 ?案例详解,,方法
- python输入输出之用户名密码,,方法一: 1 # A
- python笔记13-多线程实战篇(tomorrow),,安装1.tomorr
- python 打印调用函数名和文件名,,import ins
评论关闭