自定义sort排序
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import sys
import Queue
import threading
import os
import time
#reload(sys)
#sys.setdefaultencoding('utf8')
def get_key_val_line(line):
info = line.split(" ")
remote_addr = info[0]
end_time = time.mktime(time.strptime(info[3],'[%d/%b/%Y:%H:%M:%S'))
cost_time = info[5]
method = info[6]
request = info[7]
status = info[9]
body_bytes_sent = info[10]
bytes_sent = info[12]
ua = info[14]
if status != "200" and status != "206":
return
#config
key = ":".join([remote_addr,ua])
#key = ":".join([remote_addr,url])
#key = ":".join([remote_addr,url,ua])
#val should not change
val = [end_time, float(cost_time), bytes_sent]
return key, val
def tsort(a, b):
return int(a[0] - b[0])
def get_speed_bytes(dlinfo):
dlinfo.sort(cmp = tsort)
total_bytes = 0
e,t = 0.0, 0.0
for i in dlinfo:
total_bytes += int(i[2])
if i[0] <= e and i[0]+i[1] > e:
t += i[1] - e
elif e < i[0]:
t += i[1]
if i[0] + i[1] > e:
e = i[1]
if t == 0:
return t, total_bytes
return total_bytes/1024/t, total_bytes
if __name__ == '__main__':
d_total = {}
while True:
line = sys.stdin.readline()
if not line:break
try:
key, val = get_key_val_line(line.strip())
if not key:
continue
except:
continue
if d_total.has_key(key):
d_total[key].append(val)
else:
d_total[key] = [val]
sys.stdout.flush()
for k, v in d_total.items():
dl_speed, total_bytes = get_speed_bytes(v)
print "%.2f\t%d\t%s"%(dl_speed,total_bytes,k)