python ftp download with progressbar
i am a new one to learn Python. Try to download by FTP. search basic code from baidu. no one tells how to show progressbar while downloading. so i try to do it myself. it works finally but need a lot parts need to be optimized.
it is like below:
ftp=FTP(IP)
ftp.login(user,pwd)
bufsize=1024
fp=open(localPath,'wb')
ftp.retrbinary('RETR '+ftpPath,fp.write,bufsize)
it works as download, but it can't show progressbar. it is very painful when you download large files. so i read the Python FTP codes and find that i can do more things in the callback function 'fp.write'
it is very helpful to get clear understand by reading the python source code. list it in the below:
def retrbinary(self, cmd, callback, blocksize=8192, rest=None):
"""Retrieve data in binary mode. A new port is created for you.
Args:
cmd: A RETR command.
callback: A single parameter callable to be called on each
block of data read.
blocksize: The maximum number of bytes to read from the
socket at one time. [default: 8192]
rest: Passed to transfercmd(). [default: None]
Returns:
The response code.
"""
self.voidcmd('TYPE I')
conn = self.transfercmd(cmd, rest)
while 1:
data = conn.recv(blocksize)
if not data:
break
callback(data)
conn.close()
return self.voidresp()
we can define a new function to get the data info.
cur=[0,] #if we use cur=0, it will report :UnboundLocalError: local variable 'cur' referenced before assignment. i don't know why too. please give me more advise.
total=ftp.size(remotePath)
def newBar(data):
curr[0]+=len(data)
print '%s / %s'%(curr[0],total)
fp.write(data)
ftp.retrbinary('RETR '+ftpPath,newBar,bufsize)
it can print basic progress bar now. i am trying tqdm to show progressbar, will update when it works.
Good news: tqdm works now.
you can get details in the official page: https://pypi.python.org/pypi/tqdm
the final code is like below:
from ftplib import FTP
from time import sleep
from tqdm import tqdm def ftpConnection(IP,user,pwd):
ftp=FTP(IP)
ftp.login(user,pwd)
return ftp def downLoad(ftp,localPath,remotePath):
# Check if the latest build in the local path, if not download it.
if os.path.isfile(localPath):
print time.ctime(), 'The file existed, Do not need to download again.'
print ftp.size(remotePath)
else:
bufsize=1024
fp=open(localPath,'wb')
total=ftp.size(remotePath)
pbar=tqdm(total=total)
def bar(data):
fp.write(data)
pbar.update(len(data))
print time.ctime(),'Begin to download: %s'%remotePath
ftp.retrbinary('RETR '+remotePath,bar,bufsize)
pbar.close()
fp.close()
print time.ctime(),'Download is finished.'
python ftp download with progressbar的更多相关文章
- python ftp操作脚本&常用函数
需求:快速进行ftp上传 ,下载,查询文件 原来直接在shell下操作: 需要[连接,输用户名,输密码,单文件操作,存在超时限制] 太过于繁琐,容易操作失败 脚本改进: 一句命令,搞定多文件上传,下载 ...
- python ftp sftp
ftp 上传下载文件 12345678910111213141516171819202122232425262728293031323334 from ftplib import FTPimport ...
- python FTP上传和下载文件
1. 连接FTP server import ftplib ftp = ftplib.FTP(ftpserver, user, passwd) 等同于 import ftplib ftp = ftpl ...
- Python FTP多线程爆破脚本
初学python, 自己编写了个FTP多线爆破小脚本代码很丑= = #!usr/bin/env python #!coding=utf-8 __author__='zhengjim' from ftp ...
- [Powershell] FTP Download File
# Config $today = Get-Date -UFormat "%Y%m%d" $LogFilePath = "d:\ftpLog_$today.txt&quo ...
- FTP Download File By Some Order List
@Echo Off REM -- Define File Filter, i.e. files with extension .RBSet FindStrArgs=/E /C:".asp&q ...
- C# show FTP Download/Upload progress
https://stackoverflow.com/questions/4591059/download-file-from-ftp-with-progress-totalbytestoreceive ...
- python ftp 上传
#!/usr/bin/python # -*-coding:utf- -*- from ftplib import FTP def ftpconnect(host,username,password) ...
- [terry笔记]python FTP
如下是作业,用python做一个ftp,主要利用socket. server端在linux下运行,在client端可以执行shell命令(静态的) 在client端输入get xxx,即可下载. 在c ...
随机推荐
- 老男孩python作业5-开发一个简单的python计算器
开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...
- 非阻塞模式ServerSocketChannel 聊天室服务器端
import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import ja ...
- bzoj2190 仪仗队
题目传送门 思路: 哪些点能被人看到,其实就是哪些点不会被其他点挡住,只要顶点的坐标互质就可以了,互质用欧拉函数算.特殊考虑一下n=1和0的情况. 欧拉函数,Φ(x)=x(1-1/p1)(1-1/p2 ...
- openLayers地图缩放的回调
//设置地图最小缩放级别为17级 map.events.register("zoomend", this, function (e) { //每次地图缩放时就会进入到这 if (m ...
- [转] CSS3垂直手风琴折叠菜单
[From] http://www.html5tricks.com/css3-ver-accordion-menu.html 之前我们已经分享过很多关于手风琴菜单了,有水平方向的,也有垂直方向的.今天 ...
- Tomcat SSL证书安装配置
[From Internet] 首先找到安装Tomcat 目录下该文件“Server.xml”,一般默认路径都是在Conf 文件夹中.然后用文本编辑器打开该文件,接着找到 <Connector ...
- 今日工作总结:jquery轮转效果的集成与前台页面banner的设计思路总结
今日做了两个项目中的两个问题,现在特来总结一下,以便分享给更多的朋友们. 1.jquery轮转效果的集成 涉及到jquery的不同版本问题,解决办法是在后缀用jQuery代替$.项目地址在:121.4 ...
- Node.js frameworks
1. Express 2. Koa 3. LoopBack egghead.io What is egghead? egghead is a group of working web developm ...
- baidumapapi点线面的绘制已离线化
百度API离线化 baidumapapi2.0商用是要收费的,开发者使用也要申请个Key. 有个项目要用到点线面的绘制功能,在百度的API示例中发现有这样js封装(DrawingManager_min ...
- 【Tensorflow】 Object_detection之liunx服务器安装部署步骤记录
环境:centos7+anaconda python3.6 步骤: 1.下载Models cd 到预存放目录下,执行: git clone https://github.com/tensorflow/ ...