-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddressForwardingManager.py
More file actions
47 lines (38 loc) · 1.35 KB
/
addressForwardingManager.py
File metadata and controls
47 lines (38 loc) · 1.35 KB
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
import os
import time
import logging
import requests
from threading import Thread
from config import ADDRESS_RECEIVER, SERVER_IDENTIFIER
class AddressForwardingWorker(Thread):
def __init__(self, caller, timer):
super().__init__()
self.caller = caller
self.timer = timer
def run(self):
counter = 0
while self.caller.flag:
if counter >= self.timer:
server_ip = os.popen('ip addr show wlan0 | grep "\<inet\>" | awk \'{ print $2 }\' | awk -F "/" \'{ print $1 }\'').read().strip()
try:
requests.post(ADDRESS_RECEIVER, json={'ip': server_ip, 'id': SERVER_IDENTIFIER})
except requests.exceptions.ConnectionError:
logging.info(f"Failed to connect to the IP recording server, trying again in {self.timer} seconds...")
counter = 0
else:
counter += 1
time.sleep(1)
class AddressForwarder:
def __init__(self, timer):
self.worker = None
self.flag = False
if SERVER_IDENTIFIER:
self._start(timer)
def _start(self, timer):
self.flag = True
self.worker = AddressForwardingWorker(self, timer)
self.worker.start()
def stop(self):
self.flag = False
if SERVER_IDENTIFIER:
self.worker.join()