Python implements simple FTP client using ftplib

Source: Internet
Author: User
Tags ftp file ftp client
This article describes how to use ftplib to implement a simple FTP client in Python. The example analyzes the settings and usage skills of the ftplib module, for more information about how to use ftplib to implement a simple FTP client, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:

#! /Usr/bin/python #-*-coding: UTF-8-*-from ftplib import FTP # load ftp module ftp = FTP () # set the variable ftp. set_debuglevel (2) # enable debug level 2 and display the ftp details. connect ("IP", "port") # connect to the ftp server and port ftp. login ("user", "password") # connect to the user name and password print ftp. getwelcome () # print out the welcome information ftp. cmd ("xxx/xxx") # Change remote directory bufsize = 1024 # set the buffer size filename = "filename.txt" # file file_handle = open (filename, "wb "). write # Open the ftp file locally in write mode. retrbinaly ("RETR filename. t Xt ", file_handle, bufsize) # receives files from the server and writes them to the local file ftp. set_debuglevel (0) # Disable the debugging mode ftp. quit # exit ftp. dir () # display the file information in the directory ftp. mkd (pathname) # Create a remote directory ftp. pwd () # return the current ftp location. rmd (dirname) # Delete the remote directory ftp. delete (filename) # delete a remote file ftp. rename (fromname, toname) # Change fromname to toname. Ftp. storbinaly ("STOR filename.txt", file_handel, bufsize) # upload the target file

A complete FTP 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 = '21'): self. ftp. set_debuglevel (2) # enable debug level 2 and display details # self. ftp. set_pasv (0) #0 Active Mode 1 # passive mode self. ftp. connect (host, port) def Login (self, user, passwd): self. ftp. login (user, passwd) print self. ftp. welcome def DownLoadFile (self, LocalFile, RemoteFile): file_handler = open (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. ftp. storbinary ('stor % s' % RemoteFile, file_handler, 4096) file_handler.close () return True def UpLoadFileTree (self, 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 # this ues callback function, that will change bIsDir value self. ftp. retrlines ('list', self. show) return self. bIsDir def close (self): self. ftp. quit () ftp = myFtp ('*********') ftp. login ('******', '******') # ftp. downLoadFile ('test. TXT ', 'Oss \ runtime. log') # OK # ftp. upLoadFile ('runtime. log', 'Oss \ runtime . Log') # OK # ftp. downLoadFileTree ('bcd', 'Oss \ ABC') # OK # ftp. upLoadFileTree ('AAA', "others \") ftp. close () print "OK! "
 

I hope this article will help you with Python programming.

Related Article

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.