The use of FTP communication module Ftplib in Python

Source: Internet
Author: User
Tags ftp login ftp client
The Ftplib module, which is installed by default in Python, defines the FTP class, where functions are limited and can be used to implement a simple FTP client for uploading or downloading files.
The FTP workflow and basic operation can refer to Protocol RFC959.

FTP Login Connection

From ftplib import FTP #加载ftp模块ftp =ftp () #设置变量ftp. Set_debuglevel (2) #打开调试级别2, show Details ftp.connect ("IP", "Port") #连接的ftp Sever and Port Ftp.login ("User", "password") #连接的用户名, password print ftp.getwelcome () #打印出欢迎信息ftp. cmd ("xxx/xxx") #更改远程目录bufsize = 1024x768 #设置的缓冲区大小filename = "Filename.txt" #需要下载的文件file_handle =open (filename, "WB"). Write #以写模式在本地打开文件ftp. Retrbinaly (" RETR filename.txt ", file_handle,bufsize) #接收服务器上文件并写入本地文件ftp. Set_debuglevel (0) #关闭调试模式ftp. Quit #退出ftp

FTP-related command operations

FTP.CWD (Pathname) #设置FTP当前操作的路径ftp. Dir () #显示目录下文件信息ftp. NLST () #获取目录下的文件ftp. MKD (pathname) #新建远程目录ftp. PWD () # Returns the current location Ftp.rmd (dirname) #删除远程目录ftp. Delete (filename) #删除远程文件ftp. Rename (FromName, ToName) #将fromname修改名称为toname. Ftp.storbinaly ("STOR filename.txt", file_handel,bufsize) #上传目标文件ftp. Retrbinary ("RETR filename.txt", File_handel, BufSize) #下载FTP文件

Instance

An upload code:

Import socketfrom ftplib import ftpftp_server= ' xx.xx.xx.xx ' ftp_user= ' xxxxx ' ftp_password= ' xxxxx ' ftp_backup_dir= ' Backup ' NewDay = Date.today () #获取今天的日期oldday = Date.today ()-timedelta (5) #获得5天前的日期newfile = '/home/backup/' + ' Backup_da Ta_ ' + str (newday.year) + '. ' + str (newday.month) + '. ' + str (newday.day) + '. Zip ' #本次备份文件名 (absolute path) Oldfile = '/home/backup/ ' + ' Backup_data_ ' + str (oldday.year) + '. ' + str (oldday.month) + '. ' + str (oldday.day) + '. Zip ' #5天前备份的文件名 (absolute path) def UPL Oad (): socket.setdefaulttimeout #超时FTP时间设置为60秒 FTP = FTP (ftp_server) print ("Login ftp ...") Try:ftp.login (ftp_  User, Ftp_password) print (Ftp.getwelcome ()) #获得欢迎信息 try:if Ftp_backup_dir in Ftp.nlst (): Print ("Found      Backup folder in FTP server, upload processing. ")        Else:print ("T found backup folder in FTP server, try to build it.")      FTP.MKD (Ftp_backup_dir) except:print ("the folder" + Ftp_backup_dir + "doesn ' t exits and can ' t be create!") Sys.exit () excEpt:print ("FTP login Failed.exit.") Sys.exit () ftp.cwd (ftp_backup_dir) #设置FTP路径 print ("Upload data ...") try:ftp.storbinary (' STOR ' + os.path.basename (NewFile), open (NewFile, ' RB '), 1024x768) #上传备份文件 except:print ("Upload failed.    Check your permission. ") Print ("Delte old file ...") Try:ftp.delete (Os.path.basename (oldfile)) #删除5天前的备份文件 Except:print ("The old file in   FTP doesn ' t exists, jumped. ") Print ("FTP upload successful.exit ...") ftp.quit () if __name__== ' __main__ ': Upload ()
  • 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.