How to Upload to an Ftp Server Using Ftplib Python
As a blogger or site owner, it'south inevitable sometimes you have to deal with FTP. A GUI client can helps a lot, only for more than circuitous tasks, you may need to utilize a script to dispense your FTP server. Uploading file with Python is easy and uncomplicated, to get started, you need to know the following 3 steps.
Pace ane. Create connection to FTP server with Python ftplib module
The ftplib module in the Python standard library is the go-to selection for ftp scripting. It'due south bascially a FTP client implementation in Python, unproblematic and like shooting fish in a barrel for mutual ftp tasks. The following code create a connection to FTP server with user name and password:
from ftplib import FTP import os import fileinput ftp = FTP( ) ftp.set_debuglevel ( 2 ) ftp.connect ( 'your ftp server' , 21 ) ftp.login ( 'username' , 'password' )
Step 2. Locate the server path
You can use the FTP "cwd" control to specify where you want to upload your file
Stride 3. Transfer file to FTP server with STOR control
The most commonly used method is storbinary:
fp = open (localfile, 'rb' ) ftp.storbinary ( 'STOR %s' % os.path.basename (localfile) , fp, 1024 ) fp.shut ( )
Discover the STOR command can accept accented server path, if the parameter is relative path, information technology will be uploaded to the path specified by cwd command.
The 1024 is the block size, that is, the chunk size the ftplib will transfer each time, the default value is 8192. Internally ftplib will read cake size of data from file and send it with sendall method of socket connexion. Essentially ftplib is just a wrapper around socket object and the FTP protocol.
Yous may wonder why we have to specify the STOR command in the first parameter because the method name already said information technology's a STOR command. Turns out, the method name is kind of misleading, what the method name really means should be uploading binary, there are several uploading methods, namely: STOR, APPE, STOU, they are very similar in that all of them take a preceding "Blazon I" command which set up the transfer mode as binary, the I means image which is the about common binary course of file gets uploaded to FTP server, to distinguish with "Blazon A" the ASCII mode, and then reading chunks from file to transport to server. The other two except STOR are rarely used, many never fifty-fifty know the existence of the 2, when information technology comes to uploading binary, information technology's almost equivalent to STOR command, or you lot tin call back of APPE and STOU are merely slightly variants of STOR.
Let'due south see some existent world examples.
This Python script will scan a directory , if in that location are new files , these files are uploaded to FTP server. This script is useful when you lot have a directory to which new files are added constantly, and you don't desire to upload it manually every time.
Instead, you lot just drop files into the directory, and run the script, it will identify the new files and only upload newly added files.
Beginning footstep is store the current file list into a text file for example "listing.txt". The script will read this file and compare to the current files list and find the newly added files.
After those files are uploaded, they are appended to "list.txt".
Hither is the script.
#encoding:utf-eight from ftplib import FTP import os import fileinput ftp = FTP( ) ftp.set_debuglevel ( ii ) ftp.connect ( 'your ftp server' , 21 ) ftp.login ( 'username' , 'countersign' ) ftp.cwd ( '/images' ) def ftp_upload(localfile, remotefile): fp = open (localfile, 'rb' ) ftp.storbinary ( 'STOR %south' % os.path.basename (localfile) , fp, 1024 ) fp.close ( ) impress "after upload " + localfile + " to " + remotefile localdir = "F:\\tmp\\images" def upload_img( file ): ftp_upload(localdir + "\\" + file , file ) lastlist = [ ] for line in fileinput.input (localdir + "\\listing.txt" ): lastlist.append (line.rstrip ( "\n" ) ) currentlist = bone.listdir (localdir) newfiles = list ( set (currentlist) - set (lastlist) ) if len (newfiles) == 0: print "No files need to upload" else: for needupload in newfiles: print "uploading " + localdir + "\\" + needupload upload_img(needupload) with open (localdir + "\\list.txt" , "a" ) every bit myfile: myfile.write (needupload + "\n" ) ftp.quit ( )
Re-create the script to PATH directory. Now you can execute the script at whatsoever time to flush new files to FTP server.
You tin can even set up a cron job to run it periodically.
Upload load files without specifying remote path
If y'all are using a FTP GUI, uploading tin can exist painful if the file is deeply nested, you must locate the file both in local driver and ftp server. You don't have to. Hither is a script that allows y'all ignore the path, wherever you lot are, you can merely type the script name and file proper name and the file will exist uploaded to right place.
Suppose your local projection root directory is "C:\\webprojects\\myproject" and remote is "/domains/domain.com/public_html"
ftpauto.py
def get_remote_path_from_localpath(path): path = str.replace (path, "C:\\webprojects\\myproject" , "" ) path = str.replace (path, "\\" , "/" ) render "/domains/domain.com/public_html" + path def ftp_upload(localfile, remotefile): fp = open (localfile, 'rb' ) ftp.storbinary ( 'STOR %south' % remotefile, fp, 1024 ) fp.close ( ) print ( "after upload " + localfile + " to " + remotefile)
To use it, cd to the directory in shell and execute ftpauto commodity.php.
Improve ftpauto create directory when information technology doesn't exist on ftp server
The storbinary simply works for the path already existed on ftp server, sometimes you just want to upload it anyway, if the path doesn't exist, then create it, the post-obit python script is an improvement to the ftp_upload function:
def ftp_upload(localfile, remotefile): fp = open (localfile, 'rb' ) try: ftp.storbinary ( 'STOR %s' % remotefile, fp, 1024 ) except Exception: print ( "remotefile not exist error caught" + remotefile) path,filename = os.path.split (remotefile) print ( "creating directory: " + remotefile) ftp.mkd (path) ftp_upload(localfile, remotefile) fp.close ( ) return fp.close ( ) print ( "after upload " + localfile + " to " + remotefile)
When you try to upload a file to a non-existing directory, Python will throw an exception and study mistake: ftplib.error_perm: 550: No such file or directory. In this function, we catch the exception and create the necessary directory then phone call the ftp_upload again, and this time information technology will upload successfully.
If your FTP server is not very stable which is a common example, you tin can consider using HTTP protocol to upload file, see Upload file to HTTP Server with HTTP client and PHP backend
Source: http://makble.com/upload-new-files-to-ftp-server-with-python