Skip to content

Instantly share code, notes, and snippets.

@zmaplex
Created September 15, 2016 18:21
Show Gist options
  • Save zmaplex/f8ce56eb3ae7b2633bec7f7d86e14da7 to your computer and use it in GitHub Desktop.
Save zmaplex/f8ce56eb3ae7b2633bec7f7d86e14da7 to your computer and use it in GitHub Desktop.
# coding:utf-8
import sys, time, os,urllib,re
def get_net_data(interface):
for line in open('/proc/net/dev', 'r'):
if line.split(':')[0].find(interface)>=0:
return map(int, line.split(':')[1].split())
def stop_limit():
os.system('sudo /sbin/tc qdisc dele dev eth0 root')
return 'cancaled'
def start_limit ():
os.system('/sbin/tc qdisc add dev eth0 root handle 1: htb default 25')
os.system('/sbin/tc class add dev eth0 parent 1: classid 1:1 htb rate 500kbps')
os.system('/sbin/tc qdisc add dev eth0 parent 1:1 netem delay 20ms 0ms')
os.system('/sbin/tc filter add dev eth0 parent 1: protocol ip prio 1 u32 match ip dst 0.0.0.0/0 flowid 1:1')
return 'apply'
'''
/sbin/tc qdisc add dev eth0 root handle 1: htb default 25
/sbin/tc class add dev eth0 parent 1: classid 1:1 htb rate 200kbps
/sbin/tc qdisc add dev eth0 parent 1:1 netem delay 20ms 0ms
/sbin/tc filter add dev eth0 parent 1: protocol ip prio 1 u32 match ip dst 0.0.0.0/0 flowid 1:1
/sbin/tc qdisc dele dev eth0 root
'''
def Get_net():
interface = 'eth0'
net_data = get_net_data(interface)
#receive = int(net_data[0])/ 1048576 #接受总流量
transmit = int(net_data[8]) / 1048576#发送总流量
return transmit
if __name__ == '__main__':
ago_transmit = Get_net()
now_time = ago_time = time.time()
stats = stop_limit()
bool = True
while bool:
transmit = Get_net()
now_time = time.time()
df_tx = transmit - ago_transmit
df_tm = now_time - ago_time
now_time = time.time()
if df_tx >= 7:
stats = start_limit()
ago_transmit = transmit
else:
ago_transmit = transmit
if df_tm >=30:
stats = stop_limit()
ago_time = now_time
#bool = False
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment