FTP upload and download in Python

Source: Internet
Author: User

FTP upload and download in Python

This simple template can be used to write this article conveniently when you need to upload or download it regularly.

FTP Upload in 1.python

#/usr/bin/python # -*- coding: utf-8 -*- import datetimeimport  Timeimport sysimport osimport ftplibdef upload_file ():     ftp =  ftplib. FTP ()     today=time.strftime ('%y-%m-%d ')     FTPIP=  "10.10.10.10"                                               #FTP地址     FTPPORT= 21                                                        #FTP端口号      username=  "User"                          #FTP用户名     USERPWD=  "passwd"                            #FTP密码     ftp.connect (ftpip, ftpport)      Ftp.login (username,userpwd)     FTP.SET_PASV (0)                              #打开主动模式     CURRTPATH=  "log"     lpath= "/data/python/study/"     FTP.CWD (Currtpath)     os.chdir (Lpath)      uploadlocalfilename=lpath+ "Code" +today+ ". txt"     f=open (uploadlocalfilename, ' RB ')      ftp.storbinary (' Stor codes.py ', f,1024)      #ftp. Storbinary (' stor %s '  % os.path.basename ( Uploadlocalfilename), f,1024)     f.close ()     ftp.close () Upload_File ()                                   #调用函数

#!/usr/bin/python# -*- coding: utf-8 -*- import sysimport ftplibimport  datetimeimport osimport commandsdef down_file ():     ftp=ftplib. FTP ()     date=datetime.date.today (). Strftime ("%y%m%d")     rpath= "hunk"     lpath= "/home/website/imoney/log"     ftpip= "10.10.10.10"      FTPPORT= 21    USERNAME=  "User"     userpwd =  "passwd"     ftp.connect (ftpip, ftpport)     ftp.login ( USERNAME,USERPWD)     FTP.SET_PASV (0)                             # Open Active mode     FTP.CWD (RPATH)     os.chdir (Lpath)      Downlocalfilename= "access% s_lOg.gz " %date    f = open (downlocalfilename,  ' WB ')      downrotefilename= "Access% s_log.gz"  %DATE    try:         ftp.retrbinary (' retr  '  + downrotefilename ,f.write,1024)      #可以下载较大文件     except Exception,e:         print  "down access% s_log.gz  is error: "  %date+str (e)         sys.exit ()     f.close ()      ftp.close ()  down_file ()                                    #调用函数


FTP upload and download in Python

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.