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.