Download directory with FTP server
ImportOs,sys fromFtplibImportFTP fromMimetypesImportguess_typenonpassive= False#passive FTP By defaultRemotesite ='192.168.191.1'Remotedir='.' #Path to FTPRemoteUser = ()#because I didn't set the password, so it's emptyLocaldir='.' #Local PathClean_all= Input ('Clean Local directory first?') [: 1]inch['y','Y']#whether to clear all local directory files#Connection PftpPrint('Connecting ...') Connection=FTP (remotesite) Connection.login (*RemoteUser) connection.cwd (remotedir)ifNONPASSIVE:CONNECTION.SET_PASV (False)#Most servers do passive#ClearifClean_all: forLocalNameinchOs.listdir (localdir):Try: Print('deleting local', LocalName) os.remove (Os.path.join (remotedir,localname))except: Print('Cannot delete', LocalName) Count=0remotefiles=Connection.nlst ()#only files in the directory can be downloaded and directories in the directory cannot be downloaded forRemoteNameinchRemotefiles[:5]: ifRemoteNameinch('.','..')or not '.' inchRemoteName:Continue #determine whether the directory, here according to the actual situation changesMimetype,encoding =Guess_type (remotename) mimetype= MimeTypeor '?/?'mimetype= Mimetype.split ('/') [0] LocalPath=Os.path.join (localdir,remotename)Print('Downing', RemoteName,' to', localpath,end=' ') Print(' as', mimetype,encodingor "') #Save File ifMimeType = ='text' andencoding = =None:localfile= Open (LocalPath,'W', encoding=connection.encoding) Callback=LambdaLine:localfile.write (line +'\ n') Connection.retrlines ('RETR'+remotename,callback)Else: LocalFile= Open (LocalPath,'WB') Connection.retrbinary ('RETR'+remotename,localfile.write) localfile.close () Count+ = 1connection.quit ()Print('Done :', Count,'file download.')
Uploading a directory with an FTP server
ImportOs,sys fromFtplibImportFTP fromMimetypesImportguess_typenonpassive= False#passive FTP By defaultRemotesite ='192.168.191.1'Remotedir='RRR' #Path to FTPRemoteUser = ()#because I didn't set the password, so it's emptyLocaldir='TTT' #Local PathClean_all= Input ('Clean Local directory first?') [: 1]inch['y','Y']#whether to clear all files from remote directory#Connection PftpPrint('Connecting ...') Connection=FTP (remotesite) Connection.login (*RemoteUser) connection.cwd (remotedir)ifNONPASSIVE:CONNECTION.SET_PASV (False)#Most servers do passive#ClearifClean_all: forRemoteNameinchconnection.nlst ():Try: Print('deleting local', RemoteName) connection.delete (remotename)except: Print('Cannot delete', RemoteName) Count=0localfiles=Os.listdir (Localdir)#only files in the directory can be downloaded and directories in the directory cannot be downloaded forLocalNameinchLocalfiles[:5]: mimetype,encoding=Guess_type (localname) mimetype= MimeTypeor '?/?'mimetype= Mimetype.split ('/') [0] LocalPath=Os.path.join (localdir,localname)Print('Downing', LocalName,' to', localpath,end=' ') Print(' as', mimetype,encodingor "') #Save File ifMimeType = ='text' andencoding = =None:localfile= Open (LocalPath,'RB') Connection.storlines ('RETR'+localname,localfile)Else: LocalFile= Open (LocalPath,'RB') Connection.storbinary ('RETR'+localname,localfile) localfile.close () Count+ = 1connection.quit ()Print('Done :', Count,'file uploaded.')
ORA-03113: End of file for communication channel-perfect solution