ftplib实现FTP上传与下载的功能

FTP server默认的端口号为21,20端口用于数据传输。

本文参考地址:https://docs.python.org/3.6/library/ftplib.html

  1. ftplib里包含FTP和FTP_TLS两个class,后者是封装了TLS安全传输协议的FTP,本文不多描述,查看官网即可,很简单。

  2. ftplib.FTP class就是一个模拟FTP协议客户端的class,可以使用此class与FTP server进行各种交互,如数据上传和下载等。

  3. FTP的数据传输有两种模式ASCII和binary格式,一般来说使用binary格式更加的普遍,适用场景较多。也因此FTP class最常用的四个method其实是:

FTP.storbinary(cmd, fp, blocksize=8192, callback=None, rest=None)
FTP.storlines(cmd, fp, callback=None)
FTP.retrbinary(cmd, callback, blocksize=8192, rest=None)
FTP.retrlines(cmd, callback=None)
其中storbinary与retrbinary就是进行二进制数据传输的method。

FTP数据下载:

一般来说,生产上会将ftp用户的访问权限限定在自己的家目录下(可通过修改/etc/vsftpd.conf来改变此行为模式)。

# The FTP class supports the with statement, e.g.:
from ftplib import FTP
with FTP('<ftp server IP>','user','passwd') as c,\
    open('<local filename/local fullpath_filename>','wb') as f:
    c.retrbinary('RETR <remote ftpserver filename>',f.write)
# 直接使用host,user,passwd参数初始化FTP,相当于执行了FTP.connect().login(),简便起见一般不这么麻烦的写
# f.closed为True可知文件已自动关闭
 

FTP class支持python context的with语法,此语法在python中是普遍推荐的,可以帮你自动处理相关对象的上下文,也就是说可以帮你自动关闭相关的对象,防止自己忘了quit or close。

retr开头的两个method主要参数是cmd和callback,前者是获取文件的命令,格式为“RETR filename”,后者即callback为回调函数,一般为openfile.write,表示将相关数据流写入一个打开的文件,文件打开的方式取决于使用的retr method,例如retrbinary()可以使用’wb’打开的文件。

FTP数据上传:

相应的数据上传的函数就是以stor开头的两个函数:storbinary与storlines,依然推荐使用前者,二进制总是比较受信任的。

from ftplib import FTP
with FTP('<ftp server IP>','user','passwd') as c,\
    open('<local filename/local fullpath_filename>','rb') as f:
    c.storbinary('STOR <remote filename/remote fullpath_filename>',f)
 

同样的,上传文件的CMD格式为“STOR 文件名”。

需要注意的是retr函数使用的是callback函数来将要下载的文件写入到本地open的文件中,而stor函数则在server端打开一个文python基础教程件,然后将要上传的数据写入。

其他:

关于FTP server常见的returncode,参考:https://kb.globalscape.com/Knowledgebase/10142/FTP-Status-and-Error-Codes

python2时FTP()对象可以不包含可以使用with子句的__enter__,__exit__方法,可以使用如下方式变通:

c = FTP('<ftp server IP>','user','passwd')
...
c.quit()

想建一个数据库技术和编程技术的交流群,用于磨炼提c#教程升技术能力,目前主要专注于Golang和Python以及TiDB,MySQL数据库,群号:231338927,建群日期:2019.04.26,截止2021.02.01人数:300人 …

FTP Library Routines Release 4.0 Thomas Pfau (tfpfau@gmail.com) June 7, 2013 This package implements a callable interface to FTP. The FTP protocol is specified in RFC 959. The library has been tested on linux, OpenVMS and Windows NT. It should also work without major modification on other POSIX systems. All programs using the library should include ftplib.h. FTP开源库。 Miscellaneous Functions FtpInit() - Initialize the library FtpSite() - Send a 'SITE' command FtpLastResponse() - Retrieve last server response FtpSysType() - Determine remote system type FtpSize() - Determine size of remote file FtpSizeLong() - Determine size of remote file FtpModDate() - Determine modification time of file FtpSetCallback() - Establish a callback function FtpClearCallback() - Remove a callback function Server Connection FtpConnect() - Connect to a remote server FtpLogin() - Login to remote machine FtpQuit() - Disconnect from remote server FtpOptions() - Set Connection Options Directory Functions FtpChdir() - Change working directory FtpMkdir() - Create a directory FtpRmdir() - Remove a directory FtpDir() - List a remote directory FtpNlst() - List a remote directory FtpCDUp() - Change to parent directory FtpPwd() - Determine current working directory File to File Transfer FtpGet() - Retreive a remote file FtpPut() - Send a local file to remote FtpDelete() - Delete a remote file FtpRename() - Rename a remote file File to Program Transfer These routines allow programs access to the data streams connected to remote files and directories. FtpAccess() - Open a remote file or directory FtpRead() - Read from remote file or directory FtpWrite() - Write to remote file FtpClose() - Close data connection Utilities qftp - Command line ftp utility
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值