Python to write a graceful downloader

Source: Internet
Author: User
This article mainly teaches you how to use Python to write a beautiful downloader, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the python to write the specific code for the download, for your reference, the specific content is as follows

#!/bin/python3 # Author:lidawei # create:2016-07-11 # version:1.0 # function Description: # Retrieve the file from the specified URL locally ########################### ########################## Import http.client import OS import threading import time import logging import unittest from Queue Import queue from Urllib.parse import Urlparse logging.basicconfig (level = logging. DEBUG, format = '% (asctime) s% (filename) s[line:% (lineno) d]% (levelname) s% (message) s ', datefmt = '%a,%d%b%Y% h:%m:%s ', filename = ' downloader_%s.log '% (time.strftime ('%y-%m-%d ')), FileMode = ' a ') class Downloader (object ): ' File Downloader ' ' url = ' filename = ' def __init__ (self, full_url_str, filename): ' ' Initialize ' Self.url = Urlpar SE (full_url_str) self.filename = filename def download (self): "' Executes the download, returns TRUE or false ' if self.url = = ' or self. url = = None or Self.filename = = "or Self.filename = = None:logging.error (' Invalid parameter for Downloader ') retur N False successed = False conn = None if Self.url. Scheme = = ' HTTPS ': conn = http.client.HTTPSConnection (self.url.netloc) else:conn = http.client.HTTPConnection (SE Lf.url.netloc) conn.request (' GET ', self.url.path) response = Conn.getresponse () if response.status = = 200:total_  Size = Response.getheader (' content-length ') total_size = (int) (total_size) if total_size > 0:finished_size = 0 file = open (Self.filename, ' WB ') If file:progress = Progress () Progress.start () and not Respo Nse.closed:buffers = Response.read (1024x768) file.write (buffers) Finished_size + = Len (buffers) progr      Ess.update (Finished_size, total_size) if Finished_size >= total_size:break # ... end while statment File.close () Progress.stop () Progress.join () else:logging.error (' Create local file%s failed '% (  Self.filename) # ... end If statment else:logging.error (' Request file%s size failed '% (self.filename)) # ... end If statment ELse:logging.error (' Http/https request failed, status code:%d '% (response.status)) # ... end If statment conn.clos E () return successed # ... end Download () method # ... end Downloader class class DataWriter (threading. Thread): filename = ' data_dict = {' offset ': 0, ' buffers_byte ': B '} queue = queue (+) __stop = False def __init __ (self, filename): self.filename = filename Threading. Thread.__init__ (self) #Override def run (self): And not Self.__stop:self.queue.get (True, 1) def put_data (Data_   dict): "' data_dict the data into the queue, data_dict is a dictionary, there are two elements: offset is offsets, buffers_byte is a binary byte string ' Self.queue.put (data_dict) def stop (self): Self.__stop = True class Progress (threading. Thread): Interval = 1 total_size = 0 finished_size = 0 old_size = 0 __stop = False def __init__ (self, interval = 0. 5): Self.interval = Interval Threading.  Thread.__init__ (self) #Override def run (self): # Logging.info (' All finished Percent speed ') print (' TotalFinished Percent speed ') and not Self.__stop:time.sleep (self.interval) if self.total_size > 0:percent = Self.finished_size/self.total_size * Speed = (self.finished_size-self.old_size)/self.interval msg = '      %12d%12d%10.2f%%%12d '% (self.total_size, self.finished_size, percent, speed) # logging.info (msg) print (msg) Self.old_size = self.finished_size else:logging.error (' total size is zero ') # ... end while statment # ... E  nd run () Method def stop (self): self.__stop = True def update (self, finished_size, total_size): self.finished_size = Finished_size Self.total_size = Total_size class Testdownloaderfunctions (unittest. TestCase): def setUp (self): print (' setUp ') def test_download (self): url = ' HTTP://DLDIR1.QQ.COM/QQFILE/QQ/QQ8.4/18 376/qq8.4.exe ' filename = ' qq8.4.exe ' dl = Downloader (URL, filename) dl.download () def tearDown (self): print (' Te Ardown ') if __name__ = = ' __main__ ': Unittest.main ()

Here is the test result:

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.