This article mainly introduces Pythonftp file uploading materials. If you need more information, refer to the following code. If you are interested in python File Uploading through ftp, refer
# Encoding = utf8from ftplib import FTP # load the ftp module IP address = '2017. 240.150.104 'user = 'webmaster @ stchat.cn 'password = '5' filename = 'zhihu.html' path = '/root/Desktop/zhihu.html' ftp = FTP () # Set the variable ftp. set_debuglevel (2) # enable debug level 2 and display the ftp details. connect (IP) # connect to the ftp server and port ftp. login (user, password) # connection username, password print ftp. getwelcome () # print out the welcome information ftp. storbinary ('stor % s' % filename, open (path, 'rb', 8192) print ('success ')
The following describes how to upload and download files through ftp in python.
Python itself comes with an FTP module to implement the upload/download function.
Import ftplib ftp = ftblib. FTP ("ftp.yourServer.com") ftp. login ("username", "password") filename = "index.html" ftp. storlines ("STOR" + filename, open (filename) filename = "app.exe" ftp. storbinary ("STOR" + filename, open (filename, "rb"), 1024 )#! /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"