用python編寫ftp客戶端程式
阿新 • • 發佈:2019-02-17
#!/usr/bin/env python
#author:Jims of www.ringkee.com
#create date: 2005/02/05
#description: Using ftplib module download a file from a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2) #開啟除錯級別2,顯示詳細資訊
ftp.connect('ftp_server','port') #連線
ftp.login('username','password') #登入,如果匿名登入則用空串代替即可
print ftp.getwelcome() #顯示ftp伺服器歡迎資訊
ftp.cwd('xxx/xxx/') #選擇操作目錄
bufsize = 1024 #設定緩衝塊大小
filename='dog.jpg'
file_handler = open(filename,'wb').write #以寫模式在本地開啟檔案
ftp.retrbinary('RETR dog.jpg',file_handler,bufsize) #接收伺服器上檔案並寫入本地檔案
ftp.set_debuglevel(0) #關閉除錯
ftp.quit() #退出ftp伺服器
下面一個上傳檔案的示例,要成功執行該指令碼,需在ftp伺服器上有上傳檔案的許可權。
#!/usr/bin/env python
#author:Jims of www.ringkee.com
#create date: 2005/02/05
#description: Using ftplib module upload a file to a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect('ftp_server','port')
ftp.login('username','password')
print ftp.getwelcome()
ftp.cwd('xxx/xxx/')
bufsize = 1024
filename='dog.jpg'
file_handler = open(filename,'rb')
ftp.storbinary('STOR dog.jpg',file_handler,bufsize) #上傳檔案
ftp.set_debuglevel(0)
file_handler.close() #關閉檔案
ftp.quit()
#author:Jims of www.ringkee.com
#create date: 2005/02/05
#description: Using ftplib module download a file from a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2) #開啟除錯級別2,顯示詳細資訊
ftp.connect('ftp_server','port') #連線
ftp.login('username','password') #登入,如果匿名登入則用空串代替即可
print ftp.getwelcome() #顯示ftp伺服器歡迎資訊
ftp.cwd('xxx/xxx/') #選擇操作目錄
bufsize = 1024 #設定緩衝塊大小
filename='dog.jpg'
file_handler = open(filename,'wb').write #以寫模式在本地開啟檔案
ftp.retrbinary('RETR dog.jpg',file_handler,bufsize) #接收伺服器上檔案並寫入本地檔案
ftp.set_debuglevel(0) #關閉除錯
ftp.quit() #退出ftp伺服器
下面一個上傳檔案的示例,要成功執行該指令碼,需在ftp伺服器上有上傳檔案的許可權。
#!/usr/bin/env python
#author:Jims of www.ringkee.com
#create date: 2005/02/05
#description: Using ftplib module upload a file to a ftp server.
from ftplib import FTP
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect('ftp_server','port')
ftp.login('username','password')
print ftp.getwelcome()
ftp.cwd('xxx/xxx/')
bufsize = 1024
filename='dog.jpg'
file_handler = open(filename,'rb')
ftp.storbinary('STOR dog.jpg',file_handler,bufsize) #上傳檔案
ftp.set_debuglevel(0)
file_handler.close() #關閉檔案
ftp.quit()