多用途客戶端協議
1、ftp
功能:上傳下載文件,刪除命名文件,創建刪除目錄,自動壓縮,保存目錄
1.代碼:html
#coding=utf-8 '''鏈接遠程ftp服務器,顯示問候語,並打印當前工做目錄''' from ftplib import FTP f = FTP('ftp.ibiblio.org') print "Welcome:", f.getwelcome() f.login()#匿名登錄 print "CWD:", f.pwd() f.quit()
運行結果:python
D:\python\python.exe E:/code/python/chap4/connect.py
Welcome: 220 ProFTPD Server
CWD: /linuxProcess finished with exit code 0編程
2.以ascii形式下載文件
ftp以兩種形式傳輸文件,ASCII和二進制文件
代碼1:服務器
#coding=utf-8 from ftplib import FTP def writeline(data): fd.write(data + "\n") #f = FTP('ftp.kernel.org') f=FTP('ftp.ibiblio.org') f.login() f.cwd('/pub/linux/kernel')#在遠處系統轉換目錄 fd = open('README', 'wt') f.retrlines('RETR README', writeline) #開始傳輸,第一個參數指定一個在遠程系統運行的命令,後一個文件名;第二個參數是函數,客戶端每收到一行數據運行一次函數; #若是省略,數據會被輸出到標準輸出設備上。 fd.close() f.quit()
運行結果:網絡
README文件:
README for kernelappWhat you'll find here: kernel sources and patchessocket
注:使用python實現一個基於socket的c/s模式文件傳輸程序。出現error: [Errno 10061]錯誤。查找資料提示是」目標機器積極拒絕鏈接」。
緣由說法不過有3種:
檢查目的地址或端口號書寫出錯。
人工檢查一下代碼即可排除。
目標防火牆未關閉。
使用ping命令,驗證目標是否有迴應,必要時可用telnet,nmap等工具嘗試驗證目標端口開放狀況。
軟件或網絡情況緣由。
查看python是否有聯網權限,當前網絡是否暢通等。
3.以二進制形式下載文件
代碼:函數
#coding=utf-8 from ftplib import FTP f = FTP('ftp.ibiblio.org') f.login() f.cwd('/pub/linux/kernel') fd = open('README', 'wb') f.retrbinary('RETR README', fd.write) #retrbinary()向指定的函數輸出整塊數據 fd.close() f.quit()
4.以高級形式下載文件
ntransfercmd()函數,瞭解傳輸文件細節;
代碼:工具
#coding=utf-8 from ftplib import FTP import sys f = FTP('ftp.ibiblio.org') f.login() f.cwd('/pub/linux/kernel') #向服務器傳輸一條指令,檢差有沒有錯 f.voidcmd("TYPE I")#TYPE I表示以二進制模式傳輸 #retrbinary()會在後臺自動執行下載指令,nsransfercmd()不會 datasock, estsize = f.ntransfercmd("RETR README") transbytes = 0 fd = open('README', 'wb') while 1: buf = datasock.recv(2048) if not len(buf): break fd.write(buf) transbytes += len(buf) sys.stdout.write("Received %d " % transbytes) if estsize: sys.stdout.write("of %d bytes (%.1f%%)\r" % \ (estsize, 100.0 * float(transbytes) / float(estsize))) else: sys.stdout.write("bytes\r") sys.stdout.flush() sys.stdout.write("\n") fd.close() datasock.close() f.voidresp()#得到服務器響應,報錯 f.quit()
運行結果:
D:\python\python.exe E:/code/python/chap4/advbinarydl.py
Received 70 of 70 bytes (100.0%)Process finished with exit code 0
5.上傳數據
storbinary() read(),storlines() readline()
#coding=utf-8 from ftplib import FTP import sys, getpass, os.path host, username, localfile, remotepath = sys.argv[1:] password = getpass.getpass("Enter password for %s on %s: " % \ (username, host)) f = FTP(host) f.login(username, password) f.cwd(remotepath) f.voidcmd("TYPE I") fd = open(localfile, 'rb') datasock, esize = f.ntransfercmd('STOR %s' % os.path.basename(localfile)) esize = os.stat(localfile)[6] transbytes = 0 while 1: buf = fd.read(2048) if not len(buf): break datasock.sendall(buf) transbytes += len(buf) sys.stdout.write("Sent %d of %d bytes (%.1f%%)\r" % (transbytes, esize, 100.0 * float(transbytes) / float(esize))) sys.stdout.flush() datasock.close()#通知服務器上傳結束 sys.stdout.write("\n") fd.close() f.voidresp() f.quit()
6.高級二進制模式上傳
#cosing=utf-8 from ftplib import FTP import sys, getpass, os.path host, username, localfile, remotepath = sys.argv[1:] password = getpass.getpass("Enter password for %s on %s: " % \ (username, host)) f = FTP(host) f.login(username, password) f.cwd(remotepath) fd = open(localfile, 'rb') f.storbinary('STOR %s' % os.path.basename(localfile), fd) #f.storline('STOR %s' % os.path.basename(localfile), fd) #以二進制形式上傳 fd.close() f.quit()
7.錯誤處理
8.掃描目錄
經過 nlst(),dir()函數
nlst()函數返回給定目錄下的一系列條目(信息)。當前位置下全部文件何目錄的列表。沒有區分文件仍是目錄。
dir()能夠從遠方服務器上返回一個目錄的列表。與unix ls -l輸出同樣。
(1)nlst()
代碼:
#coding=utf-8 from ftplib import FTP f = FTP('ftp.ibiblio.org') f.login() f.cwd('/pub/linux/kernel') entries = f.nlst() entries.sort() print "%d entries:" % len(entries) for entry in entries: print entry f.quit()
運行結果:
D:\python\python.exe E:/code/python/chap4/nlst.py
58 entries:
!INDEX
!INDEX.html
!INDEX.short.html
COPYING
LFSBOOK_ED1.iso
LFSBOOK_ED1.lsm
README
changes
config
esep-1.5.lsm
esep-1.5.tgz
getkernel-1.1.1.tar.gz
getkernel.lsm
getpatch-2.21.lsm
getpatch-2.21.tar.gz
images
irq-1.71.tar.gz
irq.lsm
iso9660-compress-2.0.tar.gz
iso9660-compress.lsm
kdebug-1.1.tgz
kernel-2.1.33.dial_on_demand_patch.gz
kernel-2.1.33.dial_on_demand_patch.lsm
kscripts-2.1.40.lsm
kscripts-2.1.40.tar.gz
linux-lite-v1.00.diff.gz
linux-lite-v1.00.lsm
linux-lite-v1.00.relnotes
linux-lite-v1.00.tar.gz
misc-cards
modremove-1.01.lsm
modremove-1.01.tar.gz
modules-2.0.0.lsm
modules-2.0.0.tar.gz
modules-3.1.3-3.1.4.diff.bz2
modules-3.1.3-3.1.4.diff.gz
modules-3.1.4.tar.bz2
modules-3.1.4.tar.gz
modules.lsm
patches
pcmcia
profil.lsm
profil.tgz
readprofile-2.0.lsm
readprofile-2.0.tar.gz
rfs-2.13-4.lsm
rfs-2.13-4.tar.bz2
sound
tapes
udma-generic-0.2.1.lsm
udma-generic-0.2.1.tar.gz
upgrade-in-a-box.2.29.lsm
upgrade-in-a-box.2.29.tar.gz
vuzkern-1.0.lsm
vuzkern.gz
xabt-1.0a.beta.README
xabt-1.0a.beta.bin.tar.gz
xabt-1.0a.beta.lsmProcess finished with exit code 0
(2)dir()
代碼:
#coding=utf-8 from ftplib import FTP f = FTP('ftp.ibiblio.org') f.login() f.cwd('/pub/linux/kernel') entries = [] f.dir(entries.append) print "%d entries:" % len(entries) for entry in entries: print entry f.quit()
運行結果:
D:\python\python.exe E:/code/python/chap4/dir.py
58 entries:
drwxrwsr-x 2 1029 704 4096 Feb 20 1997 changes
drwxrwsr-x 2 1029 704 4096 Feb 18 2000 config
-r--rw-r-- 1 1029 704 17982 Jun 26 1994 COPYING
-rw-rw-r-- 1 1029 704 1164 Dec 29 1997 esep-1.5.lsm
-rw-rw-r-- 1 1029 704 20691 Dec 29 1997 esep-1.5.tgz
-rw-rw-r-- 1 1029 704 9014 Feb 2 1998 getkernel-1.1.1.tar.gz
-rw-rw-r-- 1 1029 704 586 Jan 27 1998 getkernel.lsm
-rw-r--r-- 1 1029 704 1204 Apr 24 2004 getpatch-2.21.lsm
-rw-r--r-- 1 1029 704 3841 Apr 24 2004 getpatch-2.21.tar.gz
drwxrwsr-x 2 1029 704 4096 Feb 11 1999 images
-rw-rw-r-- 1 1029 704 2596 Nov 7 2007 !INDEX
-rw-rw-r-- 1 1029 704 9081 Nov 7 2007 !INDEX.html
-rw-rw-r-- 1 1029 704 4114 Nov 7 2007 !INDEX.short.html
-rw-r--r-- 1 1029 704 111147 Jul 14 2003 irq-1.71.tar.gz
-rw-r--r-- 1 1029 704 763 Jul 18 2003 irq.lsm
-rw-rw-r-- 1 1029 704 25314 Mar 13 1998 iso9660-compress-2.0.tar.gz
-rw-rw-r-- 1 1029 704 668 Mar 13 1998 iso9660-compress.lsm
-rw-rw-r-- 1 1029 704 5849 Feb 27 1995 kdebug-1.1.tgz
-rw-rw-r-- 1 1029 704 636 Apr 15 1997 kernel-2.1.33.dial_on_demand_patch.gz
-rw-rw-r-- 1 1029 704 519 Apr 15 1997 kernel-2.1.33.dial_on_demand_patch.lsm
-rw-rw-r-- 1 1029 704 612 May 28 1997 kscripts-2.1.40.lsm
-rw-rw-r-- 1 1029 704 20522 May 28 1997 kscripts-2.1.40.tar.gz
-rw-r--r-- 1 1029 704 707048680 Nov 21 2003 LFSBOOK_ED1.iso
-rw-r--r-- 1 1029 704 956 Nov 20 2003 LFSBOOK_ED1.lsm
-rw-rw-r-- 1 1029 704 31732 Apr 28 1998 linux-lite-v1.00.diff.gz
-rw-rw-r-- 1 1029 704 779 Apr 28 1998 linux-lite-v1.00.lsm
-rw-rw-r-- 1 1029 704 7406 Apr 28 1998 linux-lite-v1.00.relnotes
-rw-rw-r-- 1 1029 704 1259801 Apr 28 1998 linux-lite-v1.00.tar.gz
drwxrwsr-x 2 1029 704 4096 Jan 31 2001 misc-cards
-rw-rw-r-- 1 1029 704 563 Nov 2 1998 modremove-1.01.lsm
-rw-rw-r-- 1 1029 704 10367 Nov 2 1998 modremove-1.01.tar.gz
-rw-rw-r-- 1 1029 704 588 Jun 12 1996 modules-2.0.0.lsm
-rw-rw-r-- 1 1029 704 115459 Jun 12 1996 modules-2.0.0.tar.gz
-rw-rw-r-- 1 1029 704 14780 Aug 3 2001 modules-3.1.3-3.1.4.diff.bz2
-rw-rw-r-- 1 1029 704 14811 Aug 3 2001 modules-3.1.3-3.1.4.diff.gz
-rw-rw-r-- 1 1029 704 293843 Aug 3 2001 modules-3.1.4.tar.bz2
-rw-rw-r-- 1 1029 704 375034 Aug 3 2001 modules-3.1.4.tar.gz
-rw-rw-r-- 1 1029 704 1369 Aug 3 2001 modules.lsm
drwxrwsr-x 10 1029 704 4096 Feb 11 1999 patches
drwxr-xr-x 2 root root 4096 Dec 4 2006 pcmcia
-rw-rw-r-- 1 1029 704 466 Jul 13 1997 profil.lsm
-rw-rw-r-- 1 1029 704 1581 Feb 20 1995 profil.tgz
-rw-r--r-- 1 1029 704 70 Nov 7 2007 README
-rw-rw-r-- 1 1029 704 532 May 16 1996 readprofile-2.0.lsm
-rw-rw-r-- 1 1029 704 4068 May 16 1996 readprofile-2.0.tar.gz
-rw-r--r-- 1 1029 704 629 Oct 29 2007 rfs-2.13-4.lsm
-rw-r--r-- 1 1029 704 19735 Oct 29 2007 rfs-2.13-4.tar.bz2
drwxrwsr-x 2 1029 704 4096 Apr 23 2001 sound
drwxrwsr-x 2 1029 704 4096 Jul 26 2000 tapes
-rw-rw-r-- 1 1029 704 875 May 10 1998 udma-generic-0.2.1.lsm
-rw-rw-r-- 1 1029 704 14536 May 10 1998 udma-generic-0.2.1.tar.gz
-rw-rw-r-- 1 1029 704 1124 Feb 9 1997 upgrade-in-a-box.2.29.lsm
-rw-rw-r-- 1 1029 704 18479392 Feb 9 1997 upgrade-in-a-box.2.29.tar.gz
-rw-rw-r-- 1 1029 704 747 Sep 18 1995 vuzkern-1.0.lsm
-rw-rw-r-- 1 1029 704 1497 Sep 18 1995 vuzkern.gz
-rw-rw-r-- 1 1029 704 173279 May 25 1998 xabt-1.0a.beta.bin.tar.gz-rw-rw-r-- 1 1029 704 422 May 25 1998 xabt-1.0a.beta.lsm
-rw-rw-r-- 1 1029 704 735 May 25 1998 xabt-1.0a.beta.READMEProcess finished with exit code 0
9.解析unix目錄表
代碼:
#coding=utf-8 from ftplib import FTP #在一個目錄中的單獨條目 class DirEntry: def __init__(self, line): self.parts = line.split(None, 8) def isvalid(self): return len(self.parts) >= 6 def gettype(self): """Returns - for regular file; d for directory; l for symlink.""" return self.parts[0][0] def getfilename(self): if self.gettype() != 'l': return self.parts[-1] else: return self.parts[-1].split(' -> ', 1)[0] def getlinkdest(self): if self.gettype() == 'l': return self.parts[-1].split(' -> ', 1)[1] else: raise RuntimeError, "getlinkdest() called on non-link item" class DirScanner(dict): #把從dir()獲得的一行數據做爲參數,有效加到dict def addline(self, line): obj = DirEntry(line) if obj.isvalid(): self[obj.getfilename()] = obj f = FTP('ftp.ibiblio.org') f.login() f.cwd('/pub/linux/kernel') d = DirScanner() f.dir(d.addline) print "%d entries:" % len(d.keys()) for key, value in d.items(): print "%s: type %s" % (key, value.gettype()) f.quit()
運行結果:
D:\python\python.exe E:/code/python/chap4/dirparse.py
58 entries:
getkernel.lsm: type -
modules-3.1.4.tar.bz2: type -
modules.lsm: type -
linux-lite-v1.00.relnotes: type -
modremove-1.01.lsm: type -
getkernel-1.1.1.tar.gz: type -
upgrade-in-a-box.2.29.tar.gz: type -
COPYING: type -
images: type d
vuzkern-1.0.lsm: type -
pcmcia: type d
profil.tgz: type -
linux-lite-v1.00.diff.gz: type -
misc-cards: type d
kscripts-2.1.40.tar.gz: type -
modules-3.1.4.tar.gz: type -
profil.lsm: type -
modules-3.1.3-3.1.4.diff.bz2: type -
xabt-1.0a.beta.bin.tar.gz: type -
getpatch-2.21.tar.gz: type -
rfs-2.13-4.tar.bz2: type -
readprofile-2.0.lsm: type -
README: type -
linux-lite-v1.00.tar.gz: type -
config: type d
udma-generic-0.2.1.tar.gz: type -
modules-2.0.0.tar.gz: type -
!INDEX: type -
sound: type d
LFSBOOK_ED1.lsm: type -
patches: type d
esep-1.5.tgz: type -
modules-2.0.0.lsm: type -
rfs-2.13-4.lsm: type -
xabt-1.0a.beta.lsm: type -
kdebug-1.1.tgz: type -
udma-generic-0.2.1.lsm: type -
modremove-1.01.tar.gz: type -
!INDEX.html: type -
upgrade-in-a-box.2.29.lsm: type -
!INDEX.short.html: type -
vuzkern.gz: type -
irq.lsm: type -
kernel-2.1.33.dial_on_demand_patch.lsm: type -
kscripts-2.1.40.lsm: type -
tapes: type d
getpatch-2.21.lsm: type -
esep-1.5.lsm: type -
LFSBOOK_ED1.iso: type -
linux-lite-v1.00.lsm: type -
readprofile-2.0.tar.gz: type -
kernel-2.1.33.dial_on_demand_patch.gz: type -
xabt-1.0a.beta.README: type -
iso9660-compress-2.0.tar.gz: type -
modules-3.1.3-3.1.4.diff.gz: type -
changes: type d
irq-1.71.tar.gz: type -
iso9660-compress.lsm: type -Process finished with exit code 0
10.不用解析表而獲得目錄信息
代碼:
#coding=utf-8 import ftplib class DirEntry: def __init__(self, filename, ftpobj, startingdir = None): self.filename = filename if startingdir == None: startingdir = ftpobj.pwd() try: ftpobj.cwd(filename) self.filetype = 'd' ftpobj.cwd(startingdir) except ftplib.error_perm: self.filetype = '-' def gettype(self): """Returns - for regular file; d for directory.""" return self.filetype def getfilename(self): return self.filename f = ftplib.FTP('ftp.ibiblio.org') f.login() f.cwd('/pub/linux/kernel') nitems = f.nlst() items = [DirEntry(item, f, f.pwd()) for item in nitems] print "%d entries:" % len(items) for item in items: print "%s: type %s" % (item.getfilename(), item.gettype()) f.quit()
運行結果:
D:\python\python.exe E:/code/python/chap4/nlstscan.py
58 entries:
changes: type d
config: type d
images: type d
misc-cards: type d
patches: type d
pcmcia: type d
sound: type d
tapes: type d
!INDEX.html: type -
!INDEX: type -
iso9660-compress-2.0.tar.gz: type -
!INDEX.short.html: type -
COPYING: type -
LFSBOOK_ED1.iso: type -
LFSBOOK_ED1.lsm: type -
README: type -
esep-1.5.lsm: type -
esep-1.5.tgz: type -
getkernel-1.1.1.tar.gz: type -
getkernel.lsm: type -
getpatch-2.21.lsm: type -
getpatch-2.21.tar.gz: type -
irq-1.71.tar.gz: type -
irq.lsm: type -
linux-lite-v1.00.relnotes: type -
iso9660-compress.lsm: type -
kdebug-1.1.tgz: type -
kscripts-2.1.40.lsm: type -
kernel-2.1.33.dial_on_demand_patch.gz: type -
kernel-2.1.33.dial_on_demand_patch.lsm: type -
kscripts-2.1.40.tar.gz: type -
linux-lite-v1.00.diff.gz: type -
linux-lite-v1.00.lsm: type -
modules-3.1.3-3.1.4.diff.bz2: type -
linux-lite-v1.00.tar.gz: type -
modremove-1.01.lsm: type -
modremove-1.01.tar.gz: type -
modules-2.0.0.lsm: type -
modules-2.0.0.tar.gz: type -
rfs-2.13-4.lsm: type -
modules-3.1.3-3.1.4.diff.gz: type -
modules-3.1.4.tar.bz2: type -
modules-3.1.4.tar.gz: type -
modules.lsm: type -
profil.lsm: type -
profil.tgz: type -
readprofile-2.0.lsm: type -
readprofile-2.0.tar.gz: type -
udma-generic-0.2.1.lsm: type -
udma-generic-0.2.1.tar.gz: type -
upgrade-in-a-box.2.29.lsm: type -
upgrade-in-a-box.2.29.tar.gz: type -
vuzkern-1.0.lsm: type -
vuzkern.gz: type -
xabt-1.0a.beta.README: type -
xabt-1.0a.beta.bin.tar.gz: type -
xabt-1.0a.beta.lsm: type -
rfs-2.13-4.tar.bz2: type -Process finished with exit code 0
11.遞歸下載
代碼:
#coding=utf-8 from ftplib import FTP import os, sys class DirEntry: def __init__(self, line): self.parts = line.split(None, 8) def isvalid(self): return len(self.parts) >= 6 def gettype(self): return self.parts[0][0] def getfilename(self): if self.gettype() != 'l': return self.parts[-1] else: return self.parts[-1].split(' -> ', 1)[0] def getlinkdest(self): if self.gettype() == 'l': return self.parts[-1].split(' -> ', 1)[1] else: raise RuntimeError, "getlinkdest() called on non-link item" class DirScanner(dict): def addline(self, line): obj = DirEntry(line) if obj.isvalid(): self[obj.getfilename()] = obj def downloadfile(ftpobj, filename): ftpobj.voidcmd("TYPE I") datasock, estsize = ftpobj.ntransfercmd("RETR %s" % filename) transbytes = 0 fd = open(filename, 'wb') while 1: buf = datasock.recv(2048) if not len(buf): break fd.write(buf) transbytes += len(buf) sys.stdout.write("%s: Received %d " % (filename, transbytes)) if estsize: sys.stdout.write("of %d bytes (%.1f%%)\r" % (estsize, 100.0 * float(transbytes) / float(estsize))) else: sys.stdout.write("bytes\r") fd.close() datasock.close() ftpobj.voidresp() sys.stdout.write("\n") def downloaddir(ftpobj, localpath, remotepath): print "*** Processing directory", remotepath localpath = os.path.abspath(localpath) oldlocaldir = os.getcwd() if not os.path.isdir(localpath): os.mkdir(localpath) olddir = ftpobj.pwd() try: os.chdir(localpath) ftpobj.cwd(remotepath) d = DirScanner() f.dir(d.addline) for filename, entryobj in d.items(): if entryobj.gettype() == '-': downloadfile(ftpobj, filename) elif entryobj.gettype() == 'd': downloaddir(ftpobj, localpath + '/' + filename, remotepath + '/' + filename) # Re-display directory info print "*** Processing directory", remotepath finally: os.chdir(oldlocaldir) ftpobj.cwd(olddir) f = FTP('ftp.ibiblio.org') f.login() downloaddir(f, 'kernel', '/pub/linux/kernel') f.quit()
12.操做服務器上的文件和目錄 delete()刪除一個文件 rmd()刪除一個目錄 mkdir()創建目錄 rename()移動重命名文件夾