Python FTP Download File Simple example

Source: Internet
Author: User

Simple ftp download, without any abnormal judgment.


<span style= "FONT-SIZE:14PX;" >import osfrom ftplib Import ftpftp_addr = ' 10.10.0.1 ' f=ftp (ftp_addr) f.login (' Anonymous ') f.cwd ("apk_download/")    remote_file = ' 20141223140651.apk ' f.retrbinary ("RETR%s"% remote_file, open (Remote_file, "WB"). Write) </span >


Search online for a version with exception handling from

Http://www.hiadmin.org/code/python-ftp

<span style= "FONT-SIZE:14PX;" >#! encoding:utf-8# filename:ftptestdown.py# Use the Ftplib.error_perm function to print output error messages import Ftplibimport Osimport Socketremote_ Host = "ftp.kernel.org" Remote_dir = "/pub/linux/kernel/v1.0" remote_file = "patch8.gz" Def Kernelmain (): try:ftp = Ftplib. FTP (Remote_host) except (Socket.error, socket.gaierror): print "Error cannot reach '%s '"% Remote_host re Turn print ". Connected to Remote_host '%s ': "%remote_host try:ftp.login () #使用匿名账号登陆也就是anonymous except Ftplib.error_perm : print "ERROR cannot login anonymously" ftp.quit () return print "... logged in as ' anonymously '.    ."         TRY:FTP.CWD (remote_dir) #切换当前工作目录 except Ftplib.error_perm:print "error cannot CD to '%s '"% Remote_dir Ftp.quit () return print "....  Changed to '%s ' folder ... "% Remote_dir try: #传一个回调函数给retrbinary () It is called when every binary data is received Ftp.retrbinary (" RETR%s "% Remote_file, open (Remote_file, "WB"). write) except Ftplib.error_perm:print "error cannot remote_file '%s '"% remote_file os.unlink (remote_fi Le) else:print "..... Download '%s ' to CWD ... "% remote_file ftp.quit () return# call function to execute test if __name__ = =" __main__ ": Kernelmain () &lt ;/span>




Python FTP Download File Simple example

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.