zoukankan      html  css  js  c++  java
  • 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('
    ', '')
            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('
    ', '')
            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
  • 相关阅读:
    memcached事故
    总算会用sphinx生成文档了
    python tip
    抓包工具wireshark
    狗日的用户体验
    python tip
    pymmseg
    memcached事故
    windwos序列号
    7z fromat on ubuntu&replace my fujishu electric fan
  • 原文地址:https://www.cnblogs.com/beiyeren/p/4046139.html
Copyright © 2011-2022 走看看