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