Python implements simple FTP to upload and download file instances,

Source: Internet
Author: User
Tags ftp file

Python implements simple FTP to upload and download file instances,

This example describes how to upload and download files through simple FTP implemented by python. Share it with you for your reference. The details are as follows:

Python itself comes with an FTP module to implement the upload/download function.

#! /Usr/bin/env python #-*-coding: UTF-8-*-from ftplib import FTP def ftp_up (filename = "20120904.rar"): ftp = FTP () ftp. set_debuglevel (2) # enable debugging Level 2 and display details. 0 indicates disabling debugging information ftp. connect ('2017. 168.0.1 ', '21') # connect to ftp. login ('admin', 'admin') # log on. If you log on anonymously, replace it with an empty string. # print ftp. getwelcome () # display ftp server welcome information # ftp. cwd ('xxx/xxx/') # select the operation directory bufsize = 1024 # Set the buffer block size file_handler = open (filename, 'rb') # open the file ftp locally in Read mode. storbinary ('stor % s' % OS. path. basename (filename), file_handler, bufsize) # Upload File ftp. set_debuglevel (0) file_handler.close () ftp. quit () print "ftp up OK" def ftp_down (filename = "20120904.rar"): ftp = FTP () ftp. set_debuglevel (2) ftp. connect ('2017. 168.0.1 ', '21') ftp. login ('admin', 'admin') # print ftp. getwelcome () # display ftp server welcome information # ftp. cwd ('xxx/xxx/') # select the operation directory bufsize = 1024 filename = "20120904.rar" file_handler = open (filename, 'wb '). write # Open the ftp file locally in write mode. retrbinary ('retr % s' % OS. path. basename (filename), file_handler, bufsize) # receives files from the server and writes them to the local file ftp. set_debuglevel (0) file_handler.close () ftp. quit () print "ftp down OK"

I hope this article will help you with Python programming.

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.