python脚本(自定义sort排序)

自定义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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值