Python實現(xiàn)多線程HTTP下載器示例
本文將介紹使用Python編寫多線程HTTP下載器,并生成.exe可執(zhí)行文件。
環(huán)境:windows/Linux + Python2.7.x
單線程
在介紹多線程之前首先介紹單線程。編寫單線程的思路為:
1.解析url;
2.連接web服務器;
3.構造http請求包;
4.下載文件。
接下來通過代碼進行說明。
解析url
通過用戶輸入url進行解析。如果解析的路徑為空,則賦值為'/';如果端口號為空,則賦值為"80”;下載文件的文件名可根據用戶的意愿進行更改(輸入'y'表示更改,輸入其它表示不需要更改)。
下面列出幾個解析函數:
#解析host和path def analyHostAndPath(totalUrl): protocol,s1 = urllib.splittype(totalUrl) host, path = urllib.splithost(s1) if path == '': path = '/' return host, path #解析port def analysisPort(host): host, port = urllib.splitport(host) if port is None: return 80 return port #解析filename def analysisFilename(path): filename = path.split('/')[-1] if '.' not in filename: return None return filename
連接web服務器
使用socket模塊,根據解析url得到的host和port連接web服務器,代碼如下:
import socket from analysisUrl import port,host ip = socket.gethostbyname(host) s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.connect((ip, port)) print "success connected webServer?。?
構造http請求包
根據解析url得到的path, host, port構造一個HTTP請求包。
from analysisUrl import path, host, port packet = 'GET ' + path + ' HTTP/1.1\r\nHost: ' + host + '\r\n\r\n'
下載文件
根據構造的http請求包,向服務器發(fā)送文件,抓取響應報文頭部的"Content-Length"。
def getLength(self): s.send(packet) print "send success!" buf = s.recv(1024) print buf p = re.compile(r'Content-Length: (\d*)') length = int(p.findall(buf)[0]) return length, buf
下載文件并計算下載所用的時間。
def download(self): file = open(self.filename,'wb') length,buf = self.getLength() packetIndex = buf.index('\r\n\r\n') buf = buf[packetIndex+4:] file.write(buf) sum = len(buf) while 1: buf = s.recv(1024) file.write(buf) sum = sum + len(buf) if sum >= length: break print "Success!!" if __name__ == "__main__": start = time.time() down = downloader() down.download() end = time.time() print "The time spent on this program is %f s"%(end - start)
多線程
抓取響應報文頭部的"Content-Length"字段,結合線程個數,加鎖分段下載。與單線程的不同,這里將所有代碼整合為一個文件,代碼中使用更多的Python自帶模塊。
得到"Content-Length":
def getLength(self): opener = urllib2.build_opener() req = opener.open(self.url) meta = req.info() length = int(meta.getheaders("Content-Length")[0]) return length
根據得到的Length,結合線程個數劃分范圍:
def get_range(self): ranges = [] length = self.getLength() offset = int(int(length) / self.threadNum) for i in range(self.threadNum): if i == (self.threadNum - 1): ranges.append((i*offset,'')) else: ranges.append((i*offset,(i+1)*offset)) return ranges
實現(xiàn)多線程下載,在向文件寫入內容時,向線程加鎖,并使用with lock代替lock.acquire( )...lock.release( );使用file.seek( )設置文件偏移地址,保證寫入文件的準確性。
def downloadThread(self,start,end): req = urllib2.Request(self.url) req.headers['Range'] = 'bytes=%s-%s' % (start, end) f = urllib2.urlopen(req) offset = start buffer = 1024 while 1: block = f.read(buffer) if not block: break with lock: self.file.seek(offset) self.file.write(block) offset = offset + len(block) def download(self): filename = self.getFilename() self.file = open(filename, 'wb') thread_list = [] n = 1 for ran in self.get_range(): start, end = ran print 'starting:%d thread '% n n += 1 thread = threading.Thread(target=self.downloadThread,args=(start,end)) thread.start() thread_list.append(thread) for i in thread_list: i.join() print 'Download %s Success!'%(self.file) self.file.close()
運行結果:
將(*.py)文件轉化為(*.exe)可執(zhí)行文件
當寫好了一個工具,如何讓那些沒有安裝Python的人使用這個工具呢?這就需要將.py文件轉化為.exe文件。
這里用到Python的py2exe模塊,初次使用,所以對其進行介紹:
py2exe是一個將Python腳本轉換成windows上可獨立執(zhí)行的可執(zhí)行文件(*.exe)的工具,這樣,就可以不用裝Python在windows上運行這個可執(zhí)行程序。
接下來,在multiThreadDownload.py的同目錄下,創(chuàng)建mysetup.py文件,編寫:
from distutils.core import setup import py2exe setup(console=["multiThreadDownload.py"])
接著執(zhí)行命令:Python mysetup.py py2exe
生成dist文件夾,multiTjhreadDownload.exe文件位于其中,點擊運行即可:
demo下載地址:HttpFileDownload_jb51.rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python編程實現(xiàn)雙擊更新所有已安裝python模塊的方法
這篇文章主要介紹了Python編程實現(xiàn)雙擊更新所有已安裝python模塊的方法,涉及Python針對模塊操作命令的相關封裝與調用技巧,需要的朋友可以參考下2017-06-06Python實現(xiàn)windows下模擬按鍵和鼠標點擊的方法
這篇文章主要介紹了Python實現(xiàn)windows下模擬按鍵和鼠標點擊的方法,涉及Python模擬實現(xiàn)鼠標及鍵盤事件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03Python實現(xiàn)快速排序算法及去重的快速排序的簡單示例
quick sort快速排序是一種再基礎不過的排序算法,使用Python代碼寫起來相當簡潔,這里我們就來看一下Python實現(xiàn)快速排序算法及去重的快速排序的簡單示例:2016-06-06Python執(zhí)行遺傳編程gplearn庫使用實例探究
這篇文章主要為大家介紹了Python執(zhí)行遺傳編程gplearn庫使用實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01python3+PyQt5實現(xiàn)自定義流體混合窗口部件
這篇文章主要為大家詳細介紹了python3+PyQt5實現(xiàn)自定義流體混合窗口部件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Python讀取Excel表格,并同時畫折線圖和柱狀圖的方法
今天小編就為大家分享一篇Python讀取Excel表格,并同時畫折線圖和柱狀圖的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10