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 ...
随机推荐
- npm i -S -D -g 区别
npm install 本身就有一个别名 npm i npm i module_name -S 即 npm install module_name --save 写入depende ...
- Python-删除列表中重复元素的方法
1.set()方法 x = [1,2,3,4,5,1] y = list(set(x)) print(y) ``` [1, 2, 3, 4, 5] ``` 2. x = ['b','c','d','b ...
- C++ GUI Qt4编程(08)-3.2spreadsheet-resource
1. C++ GUI Qt4编程第三章,图片使用资源机制法. 2. 步骤: 2-1. 在resource文件夹下,新建images文件,存放图片. 2-2. 新建spreadsheet.qrc文件,并 ...
- Mocoserver使用介绍
10.使用flask实现mock server 测试管理 基于思维导图的用例设计 https://www.ibm.com/developerworks/cn/web/1405_liugang_moco ...
- PIE SDK地图范围设置和图层事件监听
1. 功能简介 地图范围设置的监听就是通过IMapControlEvents接口对地图的视图范围更新或者地图的分辨率发生变化进行监听,然后做出相应的操作. 图层事件的监听就是通过IActiveView ...
- PIE SDK坐标系选择
1. 功能简介 坐标系选择可以查看当前图层的坐标系信息和显示其他坐标系的信息,下面将基于PIE SDK介绍如何实现坐标系选择功能. 2. 功能实现说明 2.1. 实现思路及原理说明 第一步 加载图层并 ...
- oracle 基础知识(四) 构成
一, oracle服务 一个oracle 服务由一个oracle 实例和一个oracle数据库组成. oracle = instance + database 总体概念: 二, oracle 实例 0 ...
- GreenPlum 大数据平台--基础使用(一)
一,操作语法 01,创建数据库 --创建用户-- [gpadmin@greenplum01 ~]$ export PGDATABASE=testDB --指定数据库名字 [gpadmin@greenp ...
- EditText属性
来自http://mp.weixin.qq.com/s/Yncr0XZ4MCWZH2vzTVyYJw android:inputType=”none”android:inputType=”text”a ...
- js 判断各种数据类型 typeof 几种类型值
了解js的都知道, 有个typeof 用来判断各种数据类型,有两种写法:typeof xxx ,typeof(xxx) 如下实例: typeof 2 输出 number ...