Python's approach to using Ftplib to implement simple FTP clients

Source: Internet
Author: User

This article illustrates how Python uses Ftplib to implement a simple FTP client. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 #!/usr/bin/python #-*-coding:utf-8-*-from ftplib import FTP #加载ftp模块 ftp=ftp () #设置变量 ftp.set_debuglevel (2) #打开调试级别2, showing details Fine Information Ftp.connect ("IP", "Port") #连接的ftp Sever and Port Ftp.login ("User", "password") #连接的用户名, password print ftp.getwelcome () #打印出欢迎信息 Ftp.cmd ("xxx/xxx") #更改远程目录 bufsize=1024 #设置的缓冲区大小 filename= "filename.txt" #需要下载的文件 file_handle=open (filename, "WB"). Write #以写模式在本地打开文件 ftp.retrbinaly ("RETR filename.txt", file_handle,bufsize) #接收服务器上文件并写入本地文件 ftp.set_debuglevel (0) # Turn off debug mode ftp.quit #退出ftp ftp.dir () #显示目录下文件信息 ftp.mkd (pathname) #新建远程目录 ftp.pwd () #返回当前所在位置 Ftp.rmd (dirname) #删除远程目录 ftp.de lete (filename) #删除远程文件 ftp.rename (FromName, ToName) #将fromname修改名称为toname. Ftp.storbinaly ("STOR filename.txt", file_handel,bufsize) #上传目标文件

An FTP complete instance:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 The #coding: Utf-8 from ctypes import * Import OS import sys import ftplib class myftp:ftp = Ftplib. FTP () Bisdir = False Path = "" Def __init__ (self, host, port= '): Self.ftp.set_debuglevel (2) #打开调试级别2, displays details #self. ftp.s ET_PASV (0) #0主动模式 1 #被动模式 self.ftp.connect (host, Port) def Login (self, User, passwd): Self.ftp.login (user, passwd) pr int Self.ftp.welcome def DownLoadFile (self, LocalFile, remotefile): File_handler = open (LocalFile, ' WB ') self.ftp.retr Binary ("RETR%s"% ( RemoteFile), File_handler.write) File_handler.close () return True def uploadfile (self, LocalFile, remotefile): If OS.P Ath.isfile (localfile) = = False:return False file_handler = open (LocalFile, "RB") Self.ftp.storbinary (' STOR%s '%remot Efile, File_handler, 4096) File_handler.close () return True def uploadfiletree (self, Localdir, Remotedir): If os.path.i Sdir (localdir) = = False:return False localnames = Os.listdir (localdir) print Remotedir self.ftp.cwd (remotedir) for Local in LOCALNAMES:SRC = OS.path.join (Localdir, local) if Os.path.isdir (SRC): self. Uploadfiletree (SRC, local) else:self. UploadFile (SRC, local) self.ftp.cwd ("...") return def downloadfiletree (self, Localdir, Remotedir): If Os.path.isdir ( Localdir) = = False:os.makedirs (localdir) self.ftp.cwd (remotedir) remotenames = Self.ftp.nlst () for file in Remotenam es:local = Os.path.join (localdir, file) if Self.isdir (file): Self. Downloadfiletree (local, file) else:self. DownLoadFile (local, file) self.ftp.cwd ("...") return def show (self, list): result = List.lower (). Split ("") if self . path in and ' <dir> ' in Result:self.bIsDir = True def isdir (self, path): Self.bisdir = False Self.path = P Ath #this ues callback function, that'll change Bisdir value self.ftp.retrlines (' LIST ', self.show) return Self.bisdir def close (self): self.ftp.quit () FTP = myftp (' ******** ') ftp.login (' * * *, ' * * * *) #ftp. DownLoadFile (' * * *. TXT ', ' Othersruntime.log ') #ok #ftp. UploadFile (' Runtime.log ', ' otheRsruntime.log ') #ok #ftp. Downloadfiletree (' BCD ', ' othersabc ') #ok #ftp. Uploadfiletree (' AAA ', "others") Ftp.close () print "ok!"

I hope this article will help you with your Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.