Python-implemented simple FTP upload download file instance

Source: Internet
Author: User
The example in this paper is about the simple FTP upload and download file implemented by Python. Share to everyone for your reference. Specific as follows:

Python itself comes with an FTP module, which enables the function of uploading and downloading functions.

#!/usr/bin/env python #-*-coding:utf-8-*-from ftplib import FTP def ftp_up (filename = "20120904.rar"): Ftp=ftp () Ftp.set_debuglevel (2) #打开调试级别2, show details, 0 for off debug information ftp.connect (' 192.168.0.1 ', ' $ ') #连接 ftp.login (' admin ', ' admin ') #登录, such as The anonymous login is replaced with an empty string #print ftp.getwelcome () #显示ftp服务器欢迎信息 #ftp. CWD (' xxx/xxx/') #选择操作目录 bufsize = 1024x768 #设置缓冲块大小 file_h  Andler = open (filename, ' RB ') #以读模式在本地打开文件 ftp.storbinary (' STOR%s '% os.path.basename (filename), file_handler,bufsize) #上传文件 ftp.set_debuglevel (0) File_handler.close () ftp.quit () print "ftp up OK" def ftp_down (filename = "20120904.R Ar "): Ftp=ftp () ftp.set_debuglevel (2) ftp.connect (' 192.168.0.1 ', ' + ') ftp.login (' admin ', ' admin ') #print FTP.GETW elcome () #显示ftp服务器欢迎信息 #ftp. CWD (' xxx/xxx/') #选择操作目录 bufsize = 1024x768 filename = "20120904.rar" File_handler = Open (FileName, ' WB '). Write #以写模式在本地打开文件 ftp.retrbinary (' RETR%s '% os.path.basename (filename), file_handler,bufsize) # Receive files on the server and write to local files Ftp.set_debugLevel (0) File_handler.close () ftp.quit () print "FTP Down OK"  

Hopefully this article will help you with Python programming.

  • 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.