1 #-*-coding:utf-8-*-2 __author__='Louis'3 4 fromFtplibImportFTP5 ImportMultiprocessing6 Import Time7 8 9 defftpconnect ():TenFTP =FTP () OneTimeout = 30 APort = 22 -Ftp.connect ('localhost', Port,timeout)#connecting to an FTP server -Ftp.login ('User','Password')#Login the returnFTP - - defupload (FileName, filelocalation): -FTP =Ftpconnect () + #Print ftp.getwelcome () # Get welcome info -FTP.CWD (R"")#Setting the FTP path + forIinchRange (len (FileName)): A Print "%s The start of upload,%s"%(Time.ctime (), filename[i]) atfp = open (Filelocalation[i],'RB') - -Ftp.storbinary ('STOR'+Filename[i], FP) - fp.close () - Print "%s The end of the%s"%(Time.ctime (), filename[i]) -Ftp.quit ()#exit the FTP server in - defDownload (FileName, FilePath): toFTP =Ftpconnect () +FTP.CWD (R"")#Setting the FTP path - forIinchRange (len (FileName)): the Print "%s The start of downloading,%s"%(Time.ctime (), filename[i]) *File_handle = open (Filepath[i],'W'). Write $ Panax NotoginsengFtp.retrbinary ("RETR"+Filename[i], File_handle) - Print "%s The end of the%s"%(Time.ctime (), filename[i]) the + ftp.quit () A the + if __name__=='__main__': - #upload the video file to the server premise is in the C:\te, already has 1000 video files, their names are 1.avi, 2.avi respectively. 3.avi, ..., 1000.avi $ #p = [none]*1000 $ #filelocal = R ' c:\Te ' - # - #For i in range: the #Baiwei = i * - #filename = [None] *Wuyi #filelocalation = [None] * the #For J in range: - #Filename_pre = Baiwei + j + 1 Wu #Filename[j] = "%s.avi"% filename_pre - #Filelocalation[j] = filelocal + "\ \" + filename[j] About #Print Filelocalation $ #P[i] = multiprocessing. Process (target=upload, args= (filename, filelocalation)) - # - #For i in range: - #P[i].start () A + the #Download the video file locally, provided the FTP server already has 100 video files, their names are 1.avi, 2.avi, 3.avi, ..., 100.avi -p = [none]*100 $Filedir = R'D:\local' the forIinchRange (10): theShiwei = i * 10 theFileName = [none]*10 theFilePath = [none]*10 - forJinchRange (10): inFilename_pre = Shiwei + j + 1 theFILENAME[J] ="%s.avi"%Filename_pre theFILEPATH[J] = Filedir +"\\"+Filename[j] About PrintFilePath theP[i] = multiprocessing. Process (Target=download, args=(FileName, FilePath)) the the forIinchRange (10): + P[i].start () -
Python uploads and downloads files with multithreading