1
1
#!/usr/bin/env python
5
from optparse import OptionParser
5
10
class UserError(Exception):
9
15
subprocess.call('sudo tc ' + command, shell=True)
11
def start(delay=500, port=443):
12
tc('qdisc add dev lo root handle 1: prio')
13
tc('qdisc add dev lo parent 1:3 handle 30: netem delay %dms' % delay)
14
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
15
' dport %d 0xffff flowid 1:3' % port)
16
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
17
' sport %d 0xffff flowid 1:3' % port)
21
tc('qdisc del dev lo root')
20
UNSPECIFIED = object()
23
def get_specified_values(cls, parser, cmd_args):
24
options, args = parser.parse_args(cmd_args)
26
item for item in options.__dict__.items()
27
if item[1] is not cls.UNSPECIFIED)
31
def run_from_args(cls, cmd_args):
32
args, kwargs = cls.get_specified_values(cls.get_parser(), cmd_args)
33
cls.run(*args, **kwargs)
36
class StartCommand(Command):
40
parser = OptionParser()
42
'-d', '--delay', dest='delay', type='int',
43
default=cls.UNSPECIFIED,
44
help='Length of delay in miliseconds (each way).')
48
def run(delay=500, port=443):
49
tc('qdisc add dev lo root handle 1: prio')
50
tc('qdisc add dev lo parent 1:3 handle 30: netem delay %dms' % delay)
51
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
52
' dport %d 0xffff flowid 1:3' % port)
53
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
54
' sport %d 0xffff flowid 1:3' % port)
57
class StopCommand(Command):
61
parser = OptionParser()
66
tc('qdisc del dev lo root')
71
'start': StartCommand,
30
75
raise UserError('Must supply a command: %s.' %
31
76
', '.join(commands.keys()))
33
78
command = commands[argv[0]]
35
80
raise UserError('%s invalid. Valid commands: %s.' %
36
(argv[1], ', '.join(commands.keys())))
81
(argv[0], ', '.join(commands.keys())))
82
command.run_from_args(argv[1:])
40
85
if __name__ == "__main__":