python httplib和urllib的性能比較


httplib代碼:

        urlParseResult = urlparse(url)
        host = urlParseResult.hostname
        path = urlParseResult.path
        conn = httplib.HTTPConnection(host)
        base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
        conn.putheader("Authorization", "Basic %s" % base64string)
        conn.endheaders()  
        
        conn.request("GET", path)
      
        try:
            with open(localLogFile, "wb") as code1:     
                with contextlib.closing(conn) as conn:
                    response = conn.getresponse()
                    while True:    
                        data = response.read(defaultBlock)
                        if not len(data):
                            print str(self.logDate)+"-"+localLogFileName+"獲取成功!"
                            return
                        else: 
                            code1.write(data)
        except urllib2.HTTPError as httpError:
            if httpError.code == httplib.NOT_FOUND:
                print url+"is not found,404"
            else:
                raise

urllib代碼:

        defaultBlock = 2048
        base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
        conn = urllib2.Request(url)
        conn.add_header("Authorization", "Basic %s" % base64string)   
        try:
            with open(localLogFile, "wb") as code1:     
                with contextlib.closing(urllib2.urlopen(conn)) as result:
                    while True:    
                        data = result.read(defaultBlock)
                        if not len(data):
                            print str(self.logDate)+"-"+localLogFileName+"獲取成功!"
                            return
                        else: 
                            code1.write(data)
        except urllib2.HTTPError as httpError:
            if httpError.code == httplib.NOT_FOUND:
                print url+"is not found,404"
            else:
                raise

執行效率代碼:

from timeit import Timer
 t1 = Timer('doGetLogByConfig()', 'from __main__ import doGetLogByConfig')
print t1.timeit(1)

結果:

httplib時間:
45.4764687239
urllib時間:
64.3462849881


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM