The usage of the FTP communication module Ftplib in Python _python

Source: Internet
Author: User

The default installed Ftplib module in Python defines the FTP class, where functions are limited and can be used to implement simple FTP clients for uploading or downloading files.
FTP Workflow and basic operation can refer to the protocol RFC959.

FTP Login Connection

From ftplib import FTP #加载ftp模块
ftp=ftp () #设置变量
ftp.set_debuglevel (2) #打开调试级别2, displaying details
ftp.connect ("IP", " Port ") #连接的ftp Sever and Ports
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) #关闭调试模式
ftp.quit #退出ftp

FTP related command actions

FTP.CWD (pathname) #设置FTP当前操作的路径
ftp.dir () #显示目录下文件信息
ftp.nlst () #获取目录下的文件
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) #上传目标文件
ftp.retrbinary ("RETR filename.txt", File_ Handel,bufsize) #下载FTP文件

Instance

An upload code:
Import socket from ftplib import FTP ftp_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/' + ' Backu P_data_ ' + str (newday.year) + '. ' + str (newday.month) + '. ' + str (newday.day) + '. Zip ' #本次备份文件名 (absolute path) Oldfile = '/home/ba
 
ckup/' + ' backup_data_ ' + str (oldday.year) + '. ' + str (oldday.month) + '. ' + str (oldday.day) + '. Zip ' #5天前备份的文件名 (absolute path) def upload (): socket.setdefaulttimeout #超时FTP时间设置为60秒 FTP = FTP (ftp_server) print ("Login ftp ...") try:ft
        P.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 ("Don 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.bas Ename (NewFile), open (NewFile, ' RB '), 1024) #上传备份文件 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 ()

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.