2016年4月18日

Python 实现 FTP 上传、下载文件

Python 中,可以使用 ftplib 模块的 FTP 对象来实现 FTP 客户端功能。API 的主要用法如下:

# 例:FTP编程
from ftplib import FTP

ftp = FTP()
timeout = 30
port = 21
ftp.connect('192.168.1.188',port,timeout) # 连接FTP服务器
ftp.login('UserName','888888') # 登录
print ftp.getwelcome()  # 获得欢迎信息 
ftp.cwd('file/test')    # 设置FTP路径
list = ftp.nlst()       # 获得目录列表
for name in list:
    print(name)             # 打印文件名字
path = 'd:/data/' + name    # 文件保存路径
f = open(path,'wb')         # 打开要保存文件
filename = 'RETR ' + name   # 保存FTP文件
ftp.retrbinary(filename,f.write) # 保存FTP上的文件
ftp.delete(name)            # 删除FTP文件
ftp.storbinary('STOR '+filename, open(path, 'rb')) # 上传FTP文件
ftp.quit()                  # 退出FTP服务器

我写了两个函数,分别实现上传和下载功能。请看完整代码:

#!/usr/bin/env python
# coding: utf-8

import os
import ftplib
import socket


# 上传指定文件到 ftp 服务器
def uploadFile(dir_ftp, filename_ftp, filepath_local, host, port, username, password):
    """
    参数说明:
    * dir_ftp: 在 ftp 服务器上存储文件的路径;
    * filename_ftp: 存储到 ftp 服务器上的文件名;
    * filepath_local: 需要上传的本地源文件路径;
    * host: ftp 服务器地址(ip/域名);
    * port: ftp 服务器端口号,一般是 21;
    * username、password: 登陆 ftp 服务器时的用户名和密码。
    """
    if not os.path.exists(filepath_local):
        print '找不到指定的源文件,请检查路径配置。'
        return False
    # connect
    try:
        f = ftplib.FTP()
        f.connect(host=host, port=port)
    except (socket.error, socket.gaierror), e:
        print '----ERROR:cannot reach ' + host
        print e
        return False
    # login
    try:
        f.login(user=username, passwd=password)
    except ftplib.error_perm, e:
        print '----ERROR:cannot login to server ' + host
        print e
        f.quit()
        return False
    print '****Logged in as ' + username + ' to server ' +host
    # change folder
    try:
        f.cwd(dir_ftp)
    except ftplib.error_perm, e:
        print '----ERROR:cannot CD to %s on %s' % (dir_ftp, host)
        print e
        f.quit()
        return False
    print '**** changed to %s folder on %s' % (dir_ftp, host)
    # upload file
    try:
        f.storbinary('STOR ' + filename_ftp, open(filepath_local, 'rb'))
    except ftplib.error_perm, e:
        print '----ERROR:cannot write %s on %s' % (filename_ftp, host)
        print e
        return False
    else:
        print '****Uploaded '+ filepath_local +' to '+ host +' as '\
            + os.path.join(dir_ftp, filename_ftp)
        f.quit()
        return True


# 登陆 ftp 服务器下载文件(保存到当前目录)
def getServerFile(dir_ftp, filename, host, port, username, password):
    """
    参数说明:
    * dir_ftp: 目标文件在 ftp 服务器上路径;
    * filename: 目标文件名;
    * host: ftp 服务器地址(ip/域名);
    * port: ftp 服务器端口号,一般是 21;
    * username、password: 登陆 ftp 服务器时的用户名和密码。
    """
    if os.path.exists(filename):
        print '****the file '+ filename +' has already exist! The file will be over writed'
    # connect
    try:
        f = ftplib.FTP()
        f.connect(host=host, port=port)
    except (socket.error, socket.gaierror), e:
        print '----ERROR:cannot reach ' + host
        print e
        return False
    # login
    try:
        f.login(user=username, passwd=password)
    except ftplib.error_perm, e:
        print '----ERROR:cannot login to server ' + host
        print e
        f.quit()
        return False
    print '****Logged in as ' + username + ' to server ' +host
    # change folder
    try:
        f.cwd(dir_ftp)
    except ftplib.error_perm, e:
        print '----ERROR:cannot CD to %s on %s' % (dir_ftp, host)
        print e
        f.quit()
        return False
    print '**** changed to %s folder on %s' % (dir_ftp, host)
    # get file
    try:
        f.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
    except ftplib.error_perm, e:
        print '----ERROR:cannot read file %s on %s' % (filename, host)
        print e
        os.unlink(filename)
        return False
    else:
        print '****Downloaded '+ filename +' from '+ host +' to ' + os.getcwd()
        f.quit()
        return True
if __name__ == "__main__":
    # 下载
    getServerFile("/ftptest", "words.txt", "ftpserver.xxxxxx.com", 21, "zhangsan", "iloveu")

    # 上传
    uploadFile("/ftptest", "upload_demo.txt", "words.txt", "ftpserver.xxxxxx.com", 21, "zhangsan", "iloveu")
    print '****done'

 

参考文章:Python:FTP上传和下载文件编程


“以书为舟,遨游尘世”,
最好的免费 kindle 电子书分享站:

You may also like...

发表回复

您的电子邮箱地址不会被公开。


*