《python核心编程》读书笔记-ftplib.FTP类的方法

简单用法

交互式的一个示例

>>> from ftplib import FTP
>>> f=FTP('127.0.0.1')
>>> f.login(user='anonymous',passwd='')
'230 Login successful.'
>>> f.dir()
drwxr-xr-x    2 0        0              45 Jan 09 08:35 pub
>>> f.dir('pub')
-rw-r--r--    1 0        0               0 Jan 09 08:35 1.txt
-rw-r--r--    1 0        0               0 Jan 09 08:35 2.txt
-rw-r--r--    1 0        0               0 Jan 09 08:35 3.txt
>>> f.quit()
'221 Goodbye.'

ftplib.FTP类方法

方法描述
dir(argument[, …])显示path目录里的内容,可选参数cb是一个回调函数,会传递给retrlines()方法
nlst(argument[, …])与dir()类似,返回的文件名列表,而不是显示这些文件文件名
rmd(dirname)删除服务器上名为dirname的目录
retrbinary(cmd, callback, blocksize=8192, rest=None)与retrlines()类似,只是这个指令处理二进制文件,回调函数cb用于处理每一块下载的数据
retrlines(cmd, callback=None)给定FTP命令(如“RETR filename”),用于下载文本文件,可选的回调函数cb用于处理文件的每一行
storbinary(cmd, fp, blocksize=8192, callback=None, rest=None)与storlines()类似,只是这个指令处理二进制文件,要给定一个文件对象f,上传块大小bs默认为8kb
storlines(cmd, fp, callback=None)给定FTP命令(如“STOR filename”),用来上传文本文件,要给定一个文件对象f
rename(fromname, toname)将服务器上的文件fromname重命名为toname。
delete(filename)从服务器中删除名为filename的文件。如果成功,则返回响应的文本,否则会引发error_perm权限错误或 error_reply其他错误。
cwd(pathname)在服务器上设置当前目录
mkd(pathname)在服务器上创建一个新目录。
pwd()返回服务器上当前目录的路径名。
quit()关闭连接

客户端FTP示例程序

import ftplib
import os
import socket

HOST='192.168.253.128'  #这是我自己搭建的ftp服务器
DIRN='pub'    #下载文件的目录
FILE='1.txt'   #下载的文件

def main():
    try:
        f=ftplib.FTP(HOST)
    except (socket.error,socket.gaierror) as e:
        print('ERROR:cannot reach "%s" % HOST')
        return
    print('*** Connected to host "%s"' % HOST)

    try:
        f.login()
    except ftplib.error_perm:
        print('ERROR:cannot login anonymously')
        f.quit()
        return
    print('*** Logged in as "anonymous"')

    try:
        f.cwd(DIRN)
    except ftplib.error_perm:
        print('ERROR:cannot CD to "%s" ' % DIRN)
        f.quit()
        return
    print('*** Change to %s to CWD' % FILE)

    try:
        f.retrbinary('RETR %s' % FILE , open(FILE,'wb').write)
    except ftplib.error_perm:
        print('ERROR: cannot read file "%s"' % FILE)
        os.unlink(FILE)
    else:
        print('Downloaded "%s" to CWD' %FILE)
        f.quit()

if __name__ == '__main__':
    main()

参考文档 https://docs.python.org/3/library/ftplib.html
参考书籍 《python核心编程》

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

余额充值