python使用ftplib作ftp操做

ftplib是 Python的內置的一個標準模塊,它提供了極強大的對FTP服務器的操做,經過它咱們能夠鏈接並操做FTP服務端,開始練習:php

1、導入模塊並進行鏈接python

>>> from ftplib import FTP >>> ftp = FTP(‘ftp.yabogo.com’) >>> ftp.login(‘yourloginname’,'password’) 

FTP登陸成功服務器

鏈接到FTP可還有以下形式:ui

一、實例化並直接鏈接,ftp=FTP(host=」, user=」, passwd=」, acct=」, timeout=」)url

二、先實例ftp=FTP(), 再使用 connect(host=」, port=0, timeout=-999)鏈接,最後login(user=」, passwd=」)spa

2、查看目錄文件或更改目錄debug

>>> ftp.retrlines(‘LIST’) 

一、retrlines(cmd)是以文本形式查看當前目錄文件,可用cmd:RETR, LIST, NLST, MLSD調試

二、若是要指定查看某個目錄的文件列表,能夠用dir(dirname) ,dirname是可選參數,默認是當前目錄;rest

三、cwd(dirname), 更改目錄! Change to a directory.code

3、查看文件的大小

>>> ftp.size(‘yabogo_logo.gif’) 2452

4、ftp上傳一個文件

>>> fp=open(‘F:/test.php’,'rb’)
>>> ftp.storbinary(‘STOR test.php’,fp)

二進上傳文件成功

storbinary( cmd, fp, blocksize=8192, callback=None, rest=None)
Args:
          cmd: A STOR command.
          fp: A file-like object with a read(num_bytes) method.
          blocksize: The maximum data size to read from fp and send over
                     the connection at once.  [default: 8192]
          callback: An optional single parameter callable that is called on
                    on each block of data after it is sent.  [default: None]
          rest: Passed to transfercmd().  [default: None]

        Returns:
          The response code.

5、退出關閉,並退出FTP

>>> ftp.quit() 221 Goodbye, logging out.’

ftplib有不少可用的方法,導入模塊後可經過help()查看幫助信息。



>>> from ftplib import FTP
>>> ftp=FTP('ftp.python.org')
>>> ftp.login()
'230 Login successful.'
>>> ftp.dir()
drwxrwxr-x 7 1004 1004 512 Aug 13 01:35 pub
>>> ftp.cwd('pub')
'250 Directory successfully changed.'
>>> ftp.dir()
drwxrwxr-x 5 1000 1004 1024 Dec 24 11:04 docs.python.org
drwxrwsr-x 2 1002 1004 512 Oct 12 2001 jython
lrwx------ 1 0 1003 25 Aug 03 2001 python -> 
www.python.org/ftp/python
drwxr-xr-x 9 1018 1004 512 Feb 02 03:44 pyvault
drwxr-xr-x 2 1005 1004 512 May 06 2003 tmp
drwxrwsr-x 59 1004 1004 3072 Feb 03 14:58 
http://www.python.org/
>>> ftp.quit()
'221 Goodbye.'


下面一個下載文件的示例

#!/usr/bin/env python

#author:Jims of 
http://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 
http://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()
相關文章
相關標籤/搜索