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的更多相关文章

  1. python ftp操作脚本&常用函数

    需求:快速进行ftp上传 ,下载,查询文件 原来直接在shell下操作: 需要[连接,输用户名,输密码,单文件操作,存在超时限制] 太过于繁琐,容易操作失败 脚本改进: 一句命令,搞定多文件上传,下载 ...

  2. python ftp sftp

    ftp 上传下载文件 12345678910111213141516171819202122232425262728293031323334 from ftplib import FTPimport ...

  3. python FTP上传和下载文件

    1. 连接FTP server import ftplib ftp = ftplib.FTP(ftpserver, user, passwd) 等同于 import ftplib ftp = ftpl ...

  4. Python FTP多线程爆破脚本

    初学python, 自己编写了个FTP多线爆破小脚本代码很丑= = #!usr/bin/env python #!coding=utf-8 __author__='zhengjim' from ftp ...

  5. [Powershell] FTP Download File

    # Config $today = Get-Date -UFormat "%Y%m%d" $LogFilePath = "d:\ftpLog_$today.txt&quo ...

  6. FTP Download File By Some Order List

    @Echo Off REM -- Define File Filter, i.e. files with extension .RBSet FindStrArgs=/E /C:".asp&q ...

  7. C# show FTP Download/Upload progress

    https://stackoverflow.com/questions/4591059/download-file-from-ftp-with-progress-totalbytestoreceive ...

  8. python ftp 上传

    #!/usr/bin/python # -*-coding:utf- -*- from ftplib import FTP def ftpconnect(host,username,password) ...

  9. [terry笔记]python FTP

    如下是作业,用python做一个ftp,主要利用socket. server端在linux下运行,在client端可以执行shell命令(静态的) 在client端输入get xxx,即可下载. 在c ...

随机推荐

  1. tp5 路由定义

    路由定义 | Route::rule('路由表达式','路由地址','请求类型'); > route 目录下的任何路由定义文件都是有效的,默认的路由定义文件是 route.php > 可以 ...

  2. bzoj 1085骑士精神 迭代深搜

    题目传送门 题目大意:给出一幅棋盘,问能否复原,中文题面,不做解释. 思路:第一次写迭代深搜的题目,这道题还是挺经典的.这道题的状态很明显的每多搜一层就是多八倍,非常的多,而且又是t组输入,所以必定有 ...

  3. UESTC - 1652 递推方程

    方程很简单,每一公里往上推就行 WA了2发,忘了单通道时的特判,还有n m傻傻分不清,忘了fixed什么的我好弱啊QAQ.. #include<bits/stdc++.h> #define ...

  4. hdu 2570 贪心

    贪心的经典题型 该死的精度问题,WA了好几次,以后能用乘的绝不用除!! #include<iostream> #include<algorithm> #include<c ...

  5. ibatis配置文件中的XML解析错误The content of elements must consist of well-formed character data or markup.

    在检查过所有的标签名都没有问题的情况下. xml中的小于号属于非法字符. SQL语句中则可能需要小于号,此时就需要用<![CDATA[  ]]>将小于号包裹,如此不会被xml解析器解析. ...

  6. Yii2 执行Save()方法失败,却没有错误信息

    一般用$model->errors 就能查看到更新失败的原因,但是这次却什么错误信息都没有,最后发现是因为在模型类中定义了一个方法 public function beforeSave($ins ...

  7. ajax请求php,在返回信息前面出现了奇怪的红点点

    如果你返回的json数据带有小红点,那么前台ajax是不认的,并且老是走ajax的error方法,不走success方法,因为ajax的dataType:“json”,你指定了返回的是json格式,j ...

  8. x86的字节对齐与不对齐的问题

    比如这么一个结构体struct foo {short s;int   n;}; struct foo bar;假设bar的地址是0x12345670如果不按4字节对齐那么bar.n的地址就是0x123 ...

  9. oracle 集群RAC搭建(四)--grid部署

    安装教程:

  10. oracle 基础(一)--闪回技术

    一,闪回表初探 闪回须知: 1 使用闪回表注意如下事项: 2 3 (1)被闪回的表必须启用行移动功能 4 5 SQL> alter table dept enable row movement; ...