Use multiple threads to download files from ftp!

Source: Internet
Author: User
Use multiple threads to download files from ftp! [Copy link]
   

  • Information
Direct elevator

1#

Posted on 16:57:08| View only the author | view in reverse order

At the requirement of hongfu yesterday, a simple example of multi‑thread multipart download of files from ftp.

  1. #-*-Encoding: gb18030 -*-
  2. Import ftplib, string
  3. Import OS, sys
  4. Import threading
  5. Class myftp:
  6. Def _ init _ (self, host = '', user ='', passwd = ''):
  7. Self. Host = Host
  8. Self. User = user
  9. Self. passwd = passwd
  10. Self. filename =''
  11. Self. FTP = ftplib. FTP (host, user, passwd)
  12. Def download_by_thread (self, filename, threadnum = 1, blocksize = 8192 ):
  13. Self. filename = filename
  14. # Getting file names
  15. Onlyname = OS. Path. basename (filename)
  16. Cmd = "size" + filename
  17. # Obtain the size of the file to be downloaded
  18. Ret = self. FTP. sendcmd (CMD)
  19. Self. FTP. Quit ()
  20. # Calculate the download size of each thread when multiple threads are used for download.
  21. Fsize = int (string. Split (RET) [1])
  22. Print 'file', filename, 'size: ', fsize
  23. Rest = none
  24. Bsize = fsize/threadnum
  25. # Creating a thread
  26. Threads = []
  27. For I in range (0, threadnum-1 ):
  28. Begin = bsize * I
  29. Print I, begin, bsize
  30. TP = threading. Thread (target = self. download_file, argS = (I, filename, begin, bsize, blocksize, rest ,))
  31. Threads. append (TP)
  32. Have1 = bsize * threadnum
  33. Have2 = fsize-have1
  34. Lastsize = bsize + have2
  35. Begin = bsize * (threadnum-1)
  36. Print threadnum-1, maid, lastsize
  37. TP = threading. Thread (target = self. download_file, argS = (threadnum-1, filename, begin, lastsize, blocksize, rest ,))
  38. Threads. append (TP)
  39. Print 'threads: ', Len (threads)
  40. For T in threads:
  41. T. Start ()
  42. For T in threads:
  43. T. Join ()
  44. # Download is complete for each thread. Merge the temporary file into a file.
  45. FW = open (onlyname, "WB ")
  46. For I in range (0, threadnum ):
  47. Fname = onlyname + '. Part.' + STR (I)
  48. Print fname
  49. If not OS. Path. isfile (fname ):
  50. Print 'not found ', fname
  51. Continue
  52. F1 = open (fname, 'rb ')
  53. While 1:
  54. Data = f1.read (8192)
  55. If not Len (data ):
  56. Break
  57. FW. Write (data)
  58. F1.close ()
  59. OS. Remove (fname)
  60. FW. Close ()
  61. Print 'all OK'
  62. Def download_file (self, counter, filename, begin = 0, size = 0, blocksize = 8192, rest = none ):
  63. Onlyname = OS. Path. basename (filename)
  64. Tname = threading. currentthread (). getname ()
  65. # Splits = string. Split (tname, '-') [-1]
  66. # Create a connection to download the connection. Each thread has a connection. Note that some FTP servers have no restrictions on how many connections a single IP Address can have.
  67. Myftp = ftplib. FTP (self. Host, self. User, self. passwd)
  68. # Creating temporary files
  69. Fp = open (onlyname + '. Part.' + STR (parts), 'wb ')
  70. # FP. Seek (BEGIN)
  71. Callback = FP. Write
  72. Haveread = 0
  73. Myftp. voidcmd ('Type I ')
  74. # Tell the server where the file is to be downloaded
  75. Cmd1 = "rest" + STR (BEGIN)
  76. Print tname, cmd1
  77. Ret = myftp. sendcmd (cmd1)
  78. # Start download
  79. Cmd = "retr" + filename
  80. Conn = myftp. transfercmd (CMD, rest)
  81. Readsize = blocksize
  82. While 1:
  83. If size> 0:
  84. Last = size-haveread
  85. If last> blocksize:
  86. Readsize = blocksize
  87. Else:
  88. Readsize = last
  89. Data = conn. Recv (readsize)
  90. If not data:
  91. Break
  92. # Callback (FP, data)
  93. # Downloaded data length
  94. Haveread = haveread + Len (data)
  95. # Only data of the specified length can be downloaded. Exit upon download
  96. If haveread> size:
  97. Print tname, 'haveread: ', haveread, 'size:', size
  98. HS = haveread-size
  99. Callback (data [: Hs])
  100. Break
  101. Elif haveread = size:
  102. Callback (data)
  103. Print tname, 'haveread: ', haveread
  104. Break
  105. Callback (data)
  106. Conn. Close ()
  107. FP. Close ()
  108. Try:
  109. Ret = myftp. getresp ()
  110. Except t exception, E:
  111. Print tname, E
  112. Myftp. Quit ()
  113. Return ret
  114. FTP = myftp ("127.0.0.1", 'anonus us', '127.0.0.1 ')
  115. Filename = '/incoming/cygwin.zip'
  116. FTP. download_by_thread (filename, 10)

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.