Added conditional import of netifaces

This commit is contained in:
Mark Qvist 2021-12-01 11:39:40 +01:00
parent ea3716f48e
commit 652b884d72
2 changed files with 34 additions and 6 deletions

View File

@ -1,7 +1,6 @@
from .Interface import Interface from .Interface import Interface
import socketserver import socketserver
import threading import threading
import netifaces
import platform import platform
import socket import socket
import time import time
@ -286,10 +285,25 @@ class TCPClientInterface(Interface):
class TCPServerInterface(Interface): class TCPServerInterface(Interface):
@staticmethod @staticmethod
def get_address_for_if(name): def get_address_for_if(name):
import importlib
if importlib.find_loader('netifaces') != None:
import netifaces
return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr'] return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr']
else:
RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL)
RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL)
RNS.panic()
@staticmethod
def get_broadcast_for_if(name): def get_broadcast_for_if(name):
import importlib
if importlib.find_loader('netifaces') != None:
import netifaces
return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast'] return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast']
else:
RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL)
RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL)
RNS.panic()
def __init__(self, owner, name, device=None, bindip=None, bindport=None): def __init__(self, owner, name, device=None, bindip=None, bindport=None):
self.rxb = 0 self.rxb = 0

View File

@ -1,7 +1,6 @@
from .Interface import Interface from .Interface import Interface
import socketserver import socketserver
import threading import threading
import netifaces
import socket import socket
import time import time
import sys import sys
@ -12,10 +11,25 @@ class UDPInterface(Interface):
@staticmethod @staticmethod
def get_address_for_if(name): def get_address_for_if(name):
import importlib
if importlib.find_loader('netifaces') != None:
import netifaces
return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr'] return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['addr']
else:
RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL)
RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL)
RNS.panic()
@staticmethod
def get_broadcast_for_if(name): def get_broadcast_for_if(name):
import importlib
if importlib.find_loader('netifaces') != None:
import netifaces
return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast'] return netifaces.ifaddresses(name)[netifaces.AF_INET][0]['broadcast']
else:
RNS.log("Getting interface addresses from device names requires the netifaces module.", RNS.LOG_CRITICAL)
RNS.log("You can install it with the command: python3 -m pip install netifaces", RNS.LOG_CRITICAL)
RNS.panic()
def __init__(self, owner, name, device=None, bindip=None, bindport=None, forwardip=None, forwardport=None): def __init__(self, owner, name, device=None, bindip=None, bindport=None, forwardip=None, forwardport=None):
self.rxb = 0 self.rxb = 0