浅谈Python中ftplib模块

工作需要,需要对FTP协议下的各种文件进行上传下载操作,所以了解下Python中的ftplib模块。Python中默认安装的ftplib模块定义了FTP类,可用来实现简单的ftp客户端,用于上传或下载文件,有需要的可以看过来了。

先打个底:以理解为主,不够全面,如果看完还是不会,那一定是我写的不够好。

1.ftp登陆连接

  • 先设置FTP连接的参数
# 文件服务器参数
ftp_param = {
    'host': '10.12.8.22',
    'port': 2121,
    'user': 'user',
    'pwd': 'uesr1234',
    'points_dir': 'comm/cust_point/',
    'xsl': 'xsl/dps'
}
复制代码
  • 登录连接
# 引入FTP模块
from ftplib import FTP
import logging.config

logger = logging.getLogger(__name__)

# 设置变量
ftp=FTP()

 #连接的ftp sever和端口
ftp.connect(ftp_param['host'], ftp_param['port'])

# 登录
ftp.login(ftp_param['user'], ftp_param['pwd'])

# 打印欢迎信息
logger.debug(ftp.getwelcome())

# 进入远程目录
ftp.cwd(ftp_param['xsl'])

 # 需要下载的文件
filename="xsl.txt"

# 以写的模式在本地打开文件
file_handle=open(filename,"wb").write

# 接收服务器上文件并写入本地文件
ftp.retrbinaly("RETR xsl.txt",file_handle,bufsize) 

# 退出ftp
ftp.quit()
复制代码

2.FTP相关命令操作

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

注意:

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

3.实例

写了这么久,都只是简单的用法,缺少实例,敏感的我,读完以后仿佛感受到看这篇文章的人一股满满的期望与失落混杂的无奈感。放心~当然有实例了。代码已上传Github,代码中加入对文件内容的处理和替换,谢谢随手点赞。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值