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. 119th LeetCode Weekly Contest Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  2. JAVA 使用Dom4j 解析XML

    [转自] http://blog.csdn.net/yyywyr/article/details/38359049 解析XML的方式有很多,本文介绍使用dom4j解析xml. 1.环境准备 (1)下载 ...

  3. 合唱团---DP

    https://www.nowcoder.com/practice/661c49118ca241909add3a11c96408c8?tpId=85&tqId=29830&tPage= ...

  4. SQL注入(过滤空格和--+等注释符)

    1.地址:http://ctf5.shiyanbar.com/web/index_2.php(过滤了空格和--+等注释符) 思路:确定注入参数值类型,直接输入单引号,根据报错信息确定参数值类型为字符型 ...

  5. PIE SDK与IDL算法结合说明文档

    1.功能简介 IDL是一门简单易用的科学计算和可视化语言,包含大量的图形图像处理函数,尤其是同ENVI结合集成了该软件的大量功能,因此被广泛用于遥感.地信领域. 本示例程序实现了IDL算法与PIESD ...

  6. 完全原生javascript简约日历插件,js、html

    效果图: 效果如图所示,尽管看上去并不是很美观,但是,基本上的功能还是已经完成了,码了一天多的时间,权当做复习一下js吧. 整个做下来差不多码了500多行代码~其实只是很多的样式也包括了在其中了,虽然 ...

  7. Element UI 中国省市区级联数据

    https://www.npmjs.com/package/element-china-area-data

  8. eclipse F6和F8的问题

    # 首先大致说明一下F6和F8的作用: | 在debug模式下, F6的作用是跳到下一步,F8的作用是跳到下一个断点 # 情景: | 在eclipse以debug模式同时启动两个项目,并且两个项目都打 ...

  9. 在CentOS 7上搭建私有Docker仓库

    Hub IP:10.0.2.6 操作系统:CentOS 7 64位 Docker版本:1.12.5Client IP:10.0.2.4 操作系统:CentOS 7 64位 Docker版本:1.12. ...

  10. 突破Http协议

    突破Http协议 我到不先说什么Http什么的,对于HTTP的彻底理解是http是应用层的一个程序,就像我们写的诸多客户端和服务器模型,我们可能为了可靠,为了方便数据的解析,我们在数据包中其实就是结构 ...