python实现多线程往FTP上传文件

本文介绍了一种利用多线程提高FTP文件上传效率的方法。通过优化代码,实现了连接复用,并控制了并发线程数量,有效提升了多文件上传的速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用ftp服务是常见的文件服务,在项目利用也很多,现有项目使用ftp服务来下发文件来驱动测试。在以往过程中想通过多线程来提高多文件上传的效率,但是ftplib似乎不支持只建立一次连接,来实现多线程传输的过程。尝试修改成一次连接一次传输的多线程调用方式,可以实现,代码如下:

# coding=utf-8
# writre by qy.wu
import time
import threading
from ftplib import FTP
import os
def conn_ftp():
    ftp = FTP()
    ftp.connect(host='xx',port=21)
    ftp.login(user='xxx',passwd='xxx')
    return ftp
def ftp_upload(ftp,file_path):
    bufsize =1204
    fp = open(file_path,'rb')
    remote_startname = str(file_path).split('\\')[-1]+".now"
    remote_endname = str(file_path).split('\\')[-1]
    ftp.storbinary('STOR '+remote_startname,fp,bufsize)
    ftp.rename(remote_startname,remote_endname)
    ftp.close()
class My_Th(threading.Thread):
    def __init__(self,file_path):
        threading.Thread.__init__(self)
        self.file_path = file_path

    def run(self):
        myftp = conn_ftp()
        ftp_upload(myftp,self.file_path)

if __name__ =="__main__":
    print(time.asctime())
    thread_list =[]
    file_dir = []
    for root,dir,files in os.walk("需要上传的文件所在目录路径"):
        for i in files:
            myth = My_Th(i)
            thread_list.append(myth)
    for i in thread_list:
        i.start()
    for i in thread_list:
        i.join()
    print(time.asctime())

以上代码在win环境下可以,linux环境下修改\\为/即可,两者目录分割符不同

以上代码中的线程数量是由list的大小决定的,当文件数据过多时肯定会出问题因此,优化代码,将启动线程数量变为可控参数。想法是将列表files均分成多个list,然后在upload方法中修改传入文件路径为传入等分后的文件路径列表。主要代码如下

##这里传入的file_path需要是文件路径列表
def ftp_upload(ftp,file_path):
    bufsize = 1204
    for i in file_path:
        fp = open(i, 'rb')
        remote_startname = str(i).split('\\')[-1] + ".now"
        remote_endname = str(i).split('\\')[-1]
        ftp.storbinary('STOR ' + remote_startname, fp, bufsize)
        ftp.rename(remote_startname, remote_endname)
        ftp.close()

相应的调用方法修改如下:

if __name__ =="__main__":
    print(time.asctime())
    #启动线程的数量
    thread_num = 10
    thread_list =[]
    file_dir = []
    for root,dir,files in os.walk("需要上传的文件所在目录路径"):
        for i in files:
            file_dir.append(i)
    length = len(file_dir)
    for i in range(thread_num):
        do_list =file_dir[math.floor(i/thread_num*length):math.floor((i+1)/thread_num*length)]

        myth = My_Th(do_list)
        thread_list.append(myth)
    for i in thread_list:
        i.start()
    for i in thread_list:
        i.join()
    print(time.asctime())
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值