Python uses Ftplib to implement a simple FTP client approach

Source: Internet
Author: User
Tags ftp client
This example describes how Python implements a simple FTP client using Ftplib. Share to everyone for your reference. The implementation method is as follows:

#!/usr/bin/python #-*-coding:utf-8-*-from ftplib import FTP   #加载ftp模块 ftp=ftp ()          #设置变量 ftp.set_debuglevel (2) c2/> #打开调试级别2, Show Details 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# Open the file locally in write mode Ftp.retrbinaly ("RETR filename.txt", File_handle, bufsize) #接收服务器上文件并写入本地文件 ftp.set_debuglevel (0)    #关闭调试模式 ftp.quit          #退出ftp ftp.dir ()          #显示目录下文件信息 FTP.MKD ( Pathname)      #新建远程目录 ftp.pwd ()          #返回当前所在位置 Ftp.rmd (dirname)      #删除远程目录 ftp.delete (filename)    #删除远程文件 Ftp.rename (FromName, ToName) #将fromname修改名称为toname. Ftp.storbinaly ("STOR filename.txt", file_handel,bufsize) #上传目标文件

An FTP Full instance:

#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, show details #self. FTP.SET_PASV (0) #0主动模式 1 #被动模式 self.ftp.connect (host, Port) def Login (self, User, passwd): self.ft P.login (user, passwd) print Self.ftp.welcome def DownLoadFile (self, LocalFile, remotefile): File_handler = O Pen (localfile, ' WB ') self.ftp.retrbinary ("RETR%s"% (  RemoteFile), File_handler.write) File_handler.close () return True def uploadfile (self, localfile, remotefile ): If Os.path.isfile (localfile) = = False:return False file_handler = open (LocalFile, "RB") self.ft P.storbinary (' STOR%s '%remotefile, File_handler, 4096) File_handler.close () return True def uploadfiletree (sel F, Localdir, Remotedir): If Os.path.isdir (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 remotenames: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 result and "
 
  
   "in Result:self.bIsDir = True def isdir (self, path): Self.bisdir = False Self.path = Path #t His ues callback function, that would change Bisdir value self.ftp.retrlines (' LIST ', self.show) return SELF.BISDI R def close (self): self.ftp.quit () FTP = myftp (' ******** ') ftp. Login (' * * * * * ', ' * * * * ') #ftp. DownLoadFile (' TEST. TXT ', ' Others\\runtime.log ') #ok #ftp. UploadFile (' Runtime.log ', ' Others\\runtime.log ') #ok #ftp. Downloadfiletree (' BCD ', ' others\\abc ') #ok #ftp. Uploadfiletree (' AAA ', ' others\\ ') ftp.close () print "ok!"
  
 

Hopefully this article will help you with 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.