Python uploads and downloads files with multithreading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.