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

最新文章

  1. Python 历遍目录
  2. JTree使用
  3. 尽快写完Math!
  4. 自动存储管理 ASM (转)
  5. Uva1398 Meteor
  6. Node.js 爬虫,自动化抓取文章标题和正文
  7. 越狱Season 1-Episode 13: End of the Tunnel
  8. poj 1251 统计难题(字典树)
  9. VC++6.0连接Access数据库
  10. thymeleaf的url属性
  11. How to install Hadoop
  12. SCIP读书笔记(1)
  13. sublime text下载和汉化
  14. 高性能消息队列 CKafka 核心原理介绍(上)
  15. Spider爬虫 の 事
  16. 弹性布局--flex方向
  17. ansible-playbook(nginx例)
  18. C++入门笔记(二)变量和基本类型
  19. DSAPI多功能组件编程应用-HTTP监听服务端与客户端
  20. Linux中VSFTP的配置

热门文章

  1. 版本控制工具Git工具快速入门-Linux篇
  2. Nginx 入门指南
  3. 搭建nginx反向代理用做内网域名转发
  4. 7、Python-引用传递与值传递
  5. SQL记录-PLSQL基本语法与数据类型
  6. collectd使用
  7. CodeForces - 615D Multipliers(数论)
  8. Tomcat中配置URIEncoding="UTF-8"来处理中文的方法
  9. MongoDB服务无法启动,windows提示发生服务特定错误:100
  10. Linux下解压缩