python ftplib模块

本文介绍如何使用Python的ftplib模块进行FTP文件传输操作,包括连接、登录、上传、下载等基本功能,并提供完整的代码示例。

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

目录:

  • 函数释义
  • 下载、上传文件
  • 上传、下载文件/目录
  • 异常处理

#函数释义

from ftplib import FTP		# 加载ftp模块
ftp = FTP()					# 获取FTP对象
ftp.set_debuglevel(2)		# 打开调试级别2,显示详细信息
ftp.connect('IP', PORT)	# 连接ftp,server和端口
ftp.login('user', 'password')  # 登录用户
print(ftp.getwelcome())     # 打印欢迎信息
ftp.cmd('xxx/xxx')			# 进入远程目录
bufsize = 1024				# 设置缓存区大小
filename='filename.txt'     # 需要下载的文件
file_handle=open(filename, 'wb').write   # 以写的模式在本地打开文件
file.retrbinary('RETR filename.txt', file_handle,bufsize)  # 接收服务器上文件并写入本地文件
ftp.set_debuglevel(0)       # 关闭调试模式
ftp.quit					# 退出ftp


ftp相关的命令操作
ftp.cwd(pathname)			# 设置FTP当前操作的路径
ftp.dir()					# 显示目录下所有目录的信息
ftp.nlst()					# 获取目录下的文件
ftp.mkd(pathname)			# 新建远程目录
ftp.rmd(dirname)			# 删除远程目录
ftp.pwd()					# 返回当前所在位置
ftp.delete(filename)		# 删除远程文件
ftp.rename(fromname, toname)    #将fromname改为toname
ftp.storbinaly('STOR filename.txt',file_handel,bufsize)  # 上传目标文件 
ftp。retrbinary('RETR filename.txt',file_handel,bufsize)  # 下载FTP文件 

***FTP。quit()与FTP.close()的区别

  • FTP.quit():发送QUIT命令给服务器并关闭掉连接。这是一个比较“缓和”的关闭连接的方式,但是如果服务器对QUIT命令返回错误时,则会抛出异常。
  • FTP.close():单方面的关闭掉连接,不应该用在已经关闭的连接之后,例如不应用在FTP.quit()之后.

##例1

from ftplib import FTP
import time
import tarfile

from ftplib import FTP

def ftpconnect(host, username, password)
	ftp = FTP()
	ftp.connect(host, 21)
	ftp.login(username, password)
	return ftp

def downloadfile(ftp, remotepath, localpath):
	bufsize = 1024
	fp = open(localpath, 'wb')
	ftp.retrbinary('RETR'+remotepath, fp.write, bufsize)
	
	ftp.set_debuglevel(0)
	fp.close()

def uploadfile(ftp, remotepath, localpath):
	bufsize = 1024
	fp = open(localpath, 'rb')
	ftp.storbinary('STOR'+remotepath, fp, bufsize)
	ftp.set_debuglevel(0)
	fp.close()

if __name__ == '__main__':
	ftp = ftpconnect("******", "***", "***")
    downloadfile(ftp, "***", "***")
    uploadfile(ftp, "***", "***")

    ftp.quit()
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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值