-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathload_balancer.py
53 lines (46 loc) · 1.36 KB
/
load_balancer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psutil
from time import sleep
from commands import getstatusoutput
import os
# Edit the values below to fit your needs
adapterName = 'Broadband Connection'
SecundaryAdapterName = 'Local Area Connection'
speed_limit = 300
#
# Do not edit code below!
#
metric=1
s = os.system('netsh interface ipv4 set interface "%s" metric=%d' %(adapterName, metric))
print s
s = os.system('netsh interface ipv4 set interface "%s" metric=%d' %(SecundaryAdapterName, 25))
print s
while(True):
networkData = psutil.network_io_counters(pernic=True)
initial_bytes_sent = networkData[adapterName][0]
initial_bytes_recv = networkData[adapterName][1]
sleep(1)
networkData = psutil.network_io_counters(pernic=True)
bytes_sent = networkData[adapterName][0]
bytes_recv = networkData[adapterName][1]
diff_sent = bytes_sent - initial_bytes_sent
diff_recv = bytes_recv - initial_bytes_recv
speed = diff_recv/1024
print '%d KB/s' % speed
if speed > speed_limit:
print 'Main adapter exceeded limit'
if metric == 50:
pass
else:
metric = 50
s = os.system('netsh interface ipv4 set interface "%s" metric=%d' %(adapterName, metric))
print s
else:
print 'Main adapter stay within limit'
if metric == 1:
pass
else:
metric = 1
s = os.system('netsh interface ipv4 set interface "%s" metric=%d' %(adapterName, metric))
print s