python ftp批量下载文件_如何从Python中的ftp同时下载一些文件

I'm a newbie in Python programming.

My question is, how to download a few files at the same time. Not file by file but simultaneously from one directory on ftp. Now I use this script but I don't know how I can rebuild this code:

filenames = []

ftp.retrlines("NLST", filenames.append)

print filenames

print path

for filename in filenames:

local_filename = filename

print filename

print local_filename

f = open(local_filename, "wb")

s = ftp.size(local_filename)

sMB = s/(1024*1024)

print "file name: " + local_filename + "\nfile size: " + str(sMB) + " MB"

ftp.retrbinary("RETR %s" % local_filename, f.write)

print "\n Done :) "

time.sleep(2)

f.close()

ftp.quit() #closing connection

time.sleep(5)

It works fine, but not what I need.

解决方案

You could use multiple threads or processes. Make sure you create a new ftplib.FTP object in each thread. The simplest way (code-wise) is to use multiprocessing.Pool:

#!/usr/bin/env python

from multiprocessing.dummy import Pool # use threads

try:

from urllib import urlretrieve

except ImportError: # Python 3

from urllib.request import urlretrieve

def download(url):

url = url.strip()

try:

return urlretrieve(url, url2filename(url)), None

except Exception as e:

return None, e

if __name__ == "__main__":

p = Pool(20) # specify number of concurrent downloads

print(p.map(download, open('urls'))) # perform parallel downloads

where urls contains ftp urls for the files to download e.g., ftp://example.com/path/to/file and url2filename() extracts the filename part from an url e.g.:

import os

import posixpath

try:

from urlparse import urlsplit

from urllib import unquote

except ImportError: # Python 3

from urllib.parse import urlsplit, unquote

def url2filename(url, encoding='utf-8'):

"""Return basename corresponding to url.

>>> print url2filename('http://example.com/path/to/dir%2Ffile%C3%80?opt=1')

fileÀ

"""

urlpath = urlsplit(url).path

basename = posixpath.basename(unquote(urlpath))

if os.path.basename(basename) != basename:

raise ValueError(url) # reject 'dir%5Cbasename.ext' on Windows

return basename

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值