Skip to content

Commit 7a7e051

Browse files
committed
Hub sends a list of all ips in each request
1 parent 0f0e2f1 commit 7a7e051

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ bluepy==1.0.4
33
python-dateutil==2.5.3
44
python-dotenv==0.6.0
55
requests==2.12.4
6+
netifaces==0.10.7

src/server.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from __future__ import absolute_import, division, print_function
33
import settings
44
import time
5+
import netifaces
6+
import json
57

68
SERVER = 'http://'+settings.BADGE_SERVER_ADDR+':'+settings.BADGE_SERVER_PORT+'/'
79
PROJECTS_ENDPOINT = '{}projects'.format(SERVER)
@@ -10,6 +12,7 @@
1012
BEACONS_ENDPOINT = '{}beacons/'.format(SERVER)
1113
DATAFILES_ENDPOINT = "{}{}".format(SERVER, "{}/datafiles")
1214

15+
1316
def _badge(x):
1417
"""
1518
Generates endpoint for a given badge
@@ -35,6 +38,7 @@ def _hub(x):
3538
"""
3639
return '{}{}/'.format(HUBS_ENDPOINT, x)
3740

41+
3842
def _data(x):
3943
"""
4044
Generates endpoint for a given hub
@@ -48,13 +52,35 @@ def _data(x):
4852
DATA_ENDPOINT = _data
4953
HUB_ENDPOINT = _hub
5054

55+
56+
def get_ips():
57+
"""
58+
Generates a JSON string with the list of IPs used by the hub
59+
:return:
60+
"""
61+
interfaces = netifaces.interfaces()
62+
ips=[]
63+
for i in interfaces:
64+
if i == 'lo':
65+
continue
66+
iface = netifaces.ifaddresses(i).get(netifaces.AF_INET)
67+
if iface is not None:
68+
for j in iface:
69+
ips.append({i:j['addr']})
70+
71+
ips_json = json.dumps(ips)
72+
return ips_json
73+
74+
5175
def request_headers():
5276
"""
5377
Generate the headers to be used for all requests to server
5478
Note - all items must be strings
5579
"""
80+
print(get_ips())
5681
return {
5782
"X-APPKEY": settings.APPKEY,
5883
"X-HUB-UUID": settings.HUB_UUID,
59-
"X-HUB-TIME": str(time.time())
84+
"X-HUB-TIME": str(time.time()),
85+
"X-ALL-IPS": get_ips()
6086
}

0 commit comments

Comments
 (0)