当前位置: 首页 > 工具软件 > ftp.proxy > 使用案例 >

代理连接FTP实现上传和下载功能

寇宏义
2023-12-01

近日要实现FTP自动上传下载功能,但在办公区域受网络限制导致失败,最终研究通过办公区域的代理完成连接,并实现FTP上传和下载功能,同时将已上传和已下载的文件移动到默认的备份目录,保证定时任务不会重复获取。功能通过python实现,在网络上FTP代理登录功能可实现的比较少,贴在这里mark一下,话不多说,直接上代码。

此处param.py作为参数配置文件导入,将就不贴出来了。

# -*- coding: utf-8 -*-
'''
@author:June 六月城主
@file:ftp_proxy.py
@time:2020/2/10 11:05
'''
from ftplib import FTP
import socks
import socket
import os
import shutil
import param

'''
    通过代理连接FTP服务器,并实现上传和下载功能,完成上传或下载后,将目录下的文件移动至备份目录
'''
class ftp_proxy():
    def __init__(self,proxy_host,proxy_port,ftp_host,ftp_user,ftp_password):
        #初始化ftp参数
        self.proxy_host = proxy_host
        self.proxy_port = proxy_port
        self.ftp_host = ftp_host
        self.ftp_user = ftp_user
        self.ftp_password = ftp_password

    #设置ftp代理
    def setProxy(self):
        socks.set_default_proxy(socks.HTTP,
            self.proxy_host,
            self.proxy_port,
        )
        socket.socket = socks.socksocket

    #ftp登录
    def ftpLogin(self):
        ftp = FTP(self.ftp_host)
        ftp.set_debuglevel(1)
        ftp.login(
            user = self.ftp_user,
            passwd = self.ftp_password
        )
        return ftp


#下载FTP服务器远程目录文件,并将远程目录文件移到远程的备份目录
def downloadfile(ftp, remotepath, localpath,bakpath):
    ftp.cwd(remotepath) # 设置FTP远程目录(路径)
    list = ftp.nlst() # 获取目录下的文件,获得目录列表
    #ftp.delete('test.txt')
    for name in list:
        print(name)
        path = localpath + name # 定义文件保存路径
        f = open(path, 'wb') # 打开要保存文件
        filename = 'RETR ' + name # 保存FTP文件
        ftp.retrbinary(filename, f.write) # 保存FTP上的文件
        ftp.set_debuglevel(0) #关闭调试
        f.close()#关闭文件
        ftp.rename(name,bakpath+name)

#上传本地文件到FTP服务器,并将上传的文件移到本地备份目录
def uploadfile(ftp,remotepath, localpath,bakpath):
    bufsize = 1024
    filecount = 0
    for parent, dirnames, filenames in os.walk(localpath):
        for filename in filenames:
            if 'handle' in filename:
                filecount = filecount+1
                file = open(localpath+filename, 'rb')
                #上传目标文件
                ftp.storbinary('STOR ' + remotepath+filename, file, bufsize)
                ftp.set_debuglevel(0)
                file.close()
                #移动本地文件
                shutil.move(localpath+filename,bakpath+filename)
    print('upload '+str(filecount) + ' files')

if __name__ == '__main__':
    #获取配置文件的参数值
    #获取ftp连接参数
    ftp_params = param.ftp_params
    proxy_host = ftp_params.get('proxy_host')
    proxy_port = ftp_params.get('proxy_port')
    ftp_host = ftp_params.get('ftp_host')
    ftp_user = ftp_params.get('ftp_user')
    ftp_password = ftp_params.get('ftp_password')
    print('验证ftp_params参数:'+str(ftp_params))

    #h获取下载的目录参数
    download_path = param.download_path
    downl_remotepath =download_path.get('remotepath')
    downl_localpath = download_path.get('localpath')
    downl_bakpath = download_path.get('bakpath')
    print('验证download_path参数:' + str(download_path))

    #获取上传的目录参数
    upload_path =param.upload_path
    upload_remotepath = upload_path.get('remotepath')
    upload_localpath = upload_path.get('localpath')
    upload_bakpath = upload_path.get('bakpath')
    print('验证upload_path参数:' + str(upload_path))


    ftp_proxy = ftp_proxy(proxy_host,proxy_port,ftp_host,ftp_user,ftp_password)
    ftp_proxy.setProxy()
    ftp = ftp_proxy.ftpLogin()
    downloadfile(ftp, downl_remotepath,downl_localpath ,downl_bakpath)
    uploadfile(ftp,upload_remotepath,upload_localpath,upload_bakpath)
    ftp.quit()
    print('完成ftp上传和下载功能')
 类似资料: