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

python httplib和urllib的性能比较的更多相关文章

  1. python的httplib、urllib和urllib2的区别及用

    慢慢的把它们总结一下,总结就是最好的学习方法 宗述 首先来看一下他们的区别 urllib和urllib2 urllib 和urllib2都是接受URL请求的相关模块,但是urllib2可以接受一个Re ...

  2. Python核心模块——urllib模块

    现在Python基本入门了,现在开始要进军如何写爬虫了! 先把最基本的urllib模块弄懂吧. urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) ...

  3. [转]Python核心模块——urllib模块

    现在Python基本入门了,现在开始要进军如何写爬虫了! 先把最基本的urllib模块弄懂吧. urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) ...

  4. Python调用C模块以及性能分析

    一.c,ctypes和python的数据类型的对应关系 ctypes type ctype Python type c_char char 1-character string c_wchar wch ...

  5. [python]用profile协助程序性能优化

    转自:http://blog.csdn.net/gzlaiyonghao/article/details/1483728 本文最初发表于恋花蝶的博客http://blog.csdn.net/lanph ...

  6. Python爬虫之urllib模块2

    Python爬虫之urllib模块2 本文来自网友投稿 作者:PG-55,一个待毕业待就业的二流大学生. 看了一下上一节的反馈,有些同学认为这个没什么意义,也有的同学觉得太简单,关于Beautiful ...

  7. Python爬虫之urllib模块1

    Python爬虫之urllib模块1 本文来自网友投稿.作者PG,一个待毕业待就业二流大学生.玄魂工作室未对该文章内容做任何改变. 因为本人一直对推理悬疑比较感兴趣,所以这次爬取的网站也是平时看一些悬 ...

  8. Python爬虫之Urllib库的基本使用

    # get请求 import urllib.request response = urllib.request.urlopen("http://www.baidu.com") pr ...

  9. python学习笔记——urllib库中的parse

    1 urllib.parse urllib 库中包含有如下内容 Package contents error parse request response robotparser 其中urllib.p ...

随机推荐

  1. 使用“DiskGenius”精确隐藏硬盘坏道

    现在大家手中可能都有些有坏道的硬盘,也可能现在机器上的硬盘也出问题了.硬盘有坏道,肯定不会全部都是坏道,不能使用了.但我们因此而不能使用了,那么就太可惜了.所以,只要把有坏道的区域隐藏起来,就如同使用 ...

  2. Linux中配置Aria2 RPC Server

    启动Aria2 RPC Server 直接在终端中执行aria2c --enable-rpc --rpc-allow-origin-all可直接开启RPC服务. 这种方法并不能进行个性化的参数设置,需 ...

  3. Raspbian开启root账户

    启用root账户 使用默认用户pi,密码raspberry登录树莓派 sudo passwd root #给root账户设置密码 sudo passws --unlock root #解除root账户 ...

  4. MSXML2.ServerXMLHTTP & HTTPS & 证书过期 — msxml3.dll '80072f05'

    昨天测试一个几天前写的一个应用,时不时的报错: msxml3.dll  '80072f05' The date in the certificate is invalid or has expired ...

  5. Java:判断当前操作系统界面采用的主题是windows经典样式还是xp样式

    想起两三年前,发现写Java界面的时候,如果将当前界面的layout设为null,由于windows的不同主题界面下,标题栏的高度不一致,导致当前界面表现也不一致. 当时就想找到一个办法先判断当前用户 ...

  6. multidownloadXkcd 多线程抓图

    Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...

  7. 利用guava来实现本地的cache缓存

    guava是谷歌提供的工具类,功能强大,举个例子,我我想把数据存到本地,该咋办?我们想到的只有是全局的Map和session中.如果我们想实现这个容器的大小呢?时间呢?不好搞吧. guava就有这样的 ...

  8. 在 github 新建一个文件夹

    创建新文件的时候名字后面加个斜杠(/)就可以了.

  9. Linux记录-清空文件内容

    $ : > filename $ > filename $ echo "" > filename $ echo > filename $ cat /dev/ ...

  10. Executor, ExecutorService 和 Executors 间的不同

    java.util.concurrent.Executor, java.util.concurrent.ExecutorService, java.util.concurrent. Executors ...