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 ...
随机推荐
- 115th LeetCode Weekly Contest Prison Cells After N Days
There are 8 prison cells in a row, and each cell is either occupied or vacant. Each day, whether the ...
- HTML5必须知道的那些事
[转自] http://www.cnblogs.com/hamy/archive/2012/02/21/2362110.html 再普及一次HTML5基础,HTML5必须知道的那些事,HTML5扫盲. ...
- 全排列 next_permutation() 函数的使用
看来看去还是这篇博客比较简洁明了 https://www.cnblogs.com/My-Sunshine/p/4985366.html 顺便给出牛客网的一道题,虽然这道题用dfs写出全排列也能做,题意 ...
- c#判断一段代码运行所花费的时间
//定义一个时间对象 System.Diagnostics.Stopwatch oTime = new System.Diagnostics.Stopwatch(); oTime.Start(); / ...
- PIE SDK同态滤波
1.算法功能简介 同态滤波是减少低频增加高频,从而减少光照变化并锐化边缘或细节的图像滤波方法. 同态滤波的流程为:空间域图像→对数运算→傅里叶正变换→同态滤波――傅里叶逆变换→指数运算→同态滤波结果. ...
- QT跟VC++结合来进行插件的验证机制
由于最近公司要开发一个以C++插件机制为主的,主要有一个问题就是C++的二进制兼容性的问题.一旦类使用虚函数,只要随便改动下增删查改下头文件的虚函数,就会导致程序在跑的时候进行乱跳,因为这个时候exe ...
- 客户端与服务器cookie
认识cookie 第一部分: 概要 cookie是一种早期使用的客户端存储机制(现在仍在广泛使用),cookie数据会在Web浏览器和Web服务器之间传输, 因为早期cookie是针对服务器脚本设计的 ...
- STM32F407使用MFRC522射频卡调试及程序移植成功
版权声明:转载请注明出处,谢谢 https://blog.csdn.net/Kevin_8_Lee/article/details/88865556 或 https://www.cnblogs.co ...
- c#合并字典
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 用PHP实现同一个帐号不允许同时登陆,只允许一个帐号登录?
数据库表 user_login_info 字段:id,user_ip,user_id,last_access_time user_id 做唯一性索引 1. 用户登录后 如果没有当前用户的数据,插入一条 ...