來個基本印象
>>>from ftplib import FTP
>>>FTP = FTP('ftp.debian.org') # connect to host, default port
>>>FTP.login() # user anonymous, passwd anonymous@
'230 Login successful.'
>>>FTP.cwd('debian') # change into "debian" directory
>>>FTP.retrlines('LIST') # list directory contents
-rw-rw-r-- 1 1176 1176 1063 Jun 15 10:18 README
...
drwxr-sr-x 5 1176 1176 4096 Dec 19 2000 pool
drwxr-sr-x 4 1176 1176 4096 Nov 17 2008 project
drwxr-xr-x 3 1176 1176 4096 Oct 10 2012 tools
'226 Directory send OK.'
>>>FTP.retrbinary('RETR README', open('README', 'wb').write)
'226 Transfer complete.'
>>>FTP.QUIT()
- methods 說明
- FTP.set_debuglevel(level)
level可以是0,1,2 。數字越大,使用時給你反饋的信息就越多。
- FTP.connect(host[,PORT[,timeout]])
三個參數,依次是地址、端口、 超時時間,端口默認是21
- FTP.getwelcome()
獲取歡迎信息,也可能是服務器發送的幫助信息
- FTP.login([user[,passwd[,acct]]])
用戶名、密碼、賬戶信息(這個幾乎不用),用來登錄
- FTP.abort()
終止正在傳輸的文件,有時候不管用,但值得試一試
- FTP.sendcmd(command)
發送一個命令,返回響應信息
- FTP.set_pasv(boolean)
是否採用passive模式
- FTP.retrbinary(command,callback[,maxblocksize[,rest]])
二進制下載方式
- FTP.retrlines(command[,callback])
ascii下載方式
- FTP.storbinary(command,Documentation[,blocksize,callback,rest])
二進制上傳方式
- FTP.storlines(command,Documentation[,callback])
ascii上傳方式
- FTP.nlst(argument[,…])
只列出文件名
- FTP.dir(argument[,…])
列出文件的詳細信息
- FTP.rename(fromname,toname)
改名稱
- FTP.delete(filename)
刪除文件,如果失敗會返回錯誤信息
- FTP.cwd(pathname)
修改當前的工作目錄(在服務器上)
- FTP.mkd(pathname)
在服務器上新建一個目錄
- FTP.pwd()
顯示當前工作路徑
- FTP.rmd(dirname)
刪除目錄
- FTP.size(filename)
申請文件的大小,失敗會返回None
- FTP.quit()
先告訴服務器,禮貌的退出
- FTP.close()
單方強制關掉