Python實(shí)現(xiàn)向服務(wù)器請(qǐng)求壓縮數(shù)據(jù)及解壓縮數(shù)據(jù)的方法示例
本文實(shí)例講述了Python實(shí)現(xiàn)向服務(wù)器請(qǐng)求壓縮數(shù)據(jù)及解壓縮數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
向服務(wù)器請(qǐng)求壓縮數(shù)據(jù)格式,并解壓縮數(shù)據(jù)
#!/usr/bin/env python # encoding=utf-8 import urllib2, httplib def writeFile(fname, data): f = open(fname, "w") f.write(data) f.close() if __name__ == '__main__': httplib.HTTPConnection.debuglevel = 1 request = urllib2.Request('http://www.163.com/') request.add_header('Accept-encoding', 'gzip') # 向服務(wù)器請(qǐng)求壓縮數(shù)據(jù) opener = urllib2.build_opener() f = opener.open(request) data = f.read() # 讀取頁(yè)面返回的數(shù)據(jù) f.close() print "壓縮的數(shù)據(jù)長(zhǎng)度為:%d" %len(data) writeFile("a.html", data) import StringIO, gzip compressedstream = StringIO.StringIO(data) gziper = gzip.GzipFile(fileobj=compressedstream) data2 = gziper.read() # 讀取解壓縮后數(shù)據(jù) print "解壓縮后數(shù)據(jù)長(zhǎng)度為:%d" %len(data2) writeFile("aa.html", data2)
運(yùn)行結(jié)果:
[zcm@python #25]$./del.py 壓縮的數(shù)據(jù)長(zhǎng)度為:100457 解壓縮后數(shù)據(jù)長(zhǎng)度為:358659 [zcm@python #26]$wc *.html 4556 16010 358659 aa.html 374 2197 100457 a.html 4930 18207 459116 總用量 [zcm@python #27]$
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python URL操作技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python實(shí)現(xiàn)幾種歸一化方法(Normalization Method)
這篇文章主要介紹了python實(shí)現(xiàn)幾種歸一化方法(Normalization Method),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python遞歸查詢菜單并轉(zhuǎn)換成json實(shí)例
本篇文章主要介紹了python遞歸查詢菜單并轉(zhuǎn)換成json實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Python實(shí)現(xiàn)保證只能運(yùn)行一個(gè)腳本實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)保證只能運(yùn)行一個(gè)腳本實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-06-06Python的scikit-image模塊實(shí)例講解
在本篇文章里小編給大家整理了一篇關(guān)于Python的scikit-image模塊實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-12-12Python中多線程thread與threading的實(shí)現(xiàn)方法
這篇文章主要介紹了Python中多線程thread與threading的實(shí)現(xiàn)方法,很重要的應(yīng)用,需要的朋友可以參考下2014-08-08