Python-written multi-threaded Download tool

Source: Internet
Author: User

Actually just want to practise the usage of threading.

After writing found that the download speed has not increased much, slightly embarrassed, Khan

#-*-coding:cp936-*-import urllib2import threadingimport os,os.pathimport sysimport time,datetimeurl = ' Path = os.getc WD () file_name = ' file_fp= ' net_filesize = 0size_download = 0threadlock = Threading. Lock () Thread_num = 10block_size = 16384def get_filename_from_url (URL): Return Url[url.rfind ('/') +1:]def get_net_file_i NFO (URL): r = Urllib2.urlopen (URL) fs = Int (R.info () [' content-length ']) if R.info (). Has_key (' content-disposition ' ): fn = R.info () [' Content-disposition '].split (' filename= ') [1] fn = fn.replace (' "', '"). Replace ("'", "") E LSE:FN = Get_filename_from_url (URL) r.close () return fs,fndef file_write_init (): Global file_fp,net_files Ize,file_name net_filesize,file_name = get_net_file_info (URL) full_path= os.path.join (path,file_name) if Os.path.    Isfile (full_path) = = True:return False file_fp = File (Full_path, "WB") File_fp.truncate (net_filesize) return True def file_write (Pos,dat): Global size_download ThreadloCk.acquire () Size_download + = len (dat) #print ' size_download:%d '% (size_download) File_fp.seek (POS) FILE_FP.WR ITE (DAT) threadlock.release () def file_write_finish (): File_fp.close () class Mpdown (threading. Thread): Def __init__ (self,range_start,range_end): Threading.  Thread.__init__ (self) self.range_start = Range_start Self.range_end = Range_end def run (self): req = Urllib2. Request (URL) req.add_header (' Range ', ' bytes=%d-%d '% (self.range_start,self.range_end)) R = Urllib2.urlopen (req ) #print r.info () pos_base = Self.range_start pos = 0 Size_to_get = Self.range_end-self.rang E_start +1 while pos<size_to_get:dat = R.read (block_size) L = len (DAT) if pos+  Len (DAT) >size_to_get:dat = Dat[:size_to_get-pos] File_write (pos+pos_base,dat) POS + = Len (DAT) def download_one_thread (): Passdef download (): #check_range_acceptAble () #if file_length<thread_num use a single thread #download_one_thread () #block a size of each thread time _start = Datetime.datetime.now () if file_write_init () = = False:return print ' file name:%s '% (file_name) p Rint ' net_filesize:%dbyte '% (net_filesize) print ' Multi process downloading ... ' threads = [] Thread_size = Net_fil Esize/thread_num left_size = net_filesize% thread_num pos = 0 for I in Xrange (0,thread_num): Range_star t = pos pos + = thread_size-1 if i==0:pos + = left_size Range_end = pos pos + = 1 T = mpdo WN (Range_start,range_end) threads.append (t) #print ' range=%d-%d '% (range_start,range_end) for T in threads : T.start () for T in Threads:t.join () file_write_finish () Time_end = Datetime.datetime.now () s = (Time_end-time_start). seconds if S<1:s=1 print ' time passed:%dh%dm%ds '% (s/3600,s/60,s%60) print ' SPEED:%DK b/S '% (net_filesize/1000.0/S) def main (): global URL args = Len (sys.argv) if args>1:for i in range (1,args): url =    Sys.argv[i] Download () Else:url = raw_input ("Input download URL:") Download () def test (): req = Urllib2.  Request (URL) req.add_header (' Range ', ' bytes=%d-%d '% (1,500)) print req R = Urllib2.urlopen (req) print r.info () if __name__ = = ' __main__ ': Main ()

  

Python-written multi-threaded Download tool

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.