From a45ea080e066d856490c371ccf91c28506fa1218 Mon Sep 17 00:00:00 2001 From: William Poetra Yoga Date: Thu, 12 Dec 2024 15:10:17 +0700 Subject: [PATCH 1/2] Fix pytest issue --- requirements.txt | 2 +- setup.py | 4 ++-- wsdiscovery/threaded.py | 2 ++ wsdiscovery/util.py | 43 ++++++++++++++++++++++++++++------------- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/requirements.txt b/requirements.txt index 05f10aa..da366d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -netifaces-plus +ifaddr click diff --git a/setup.py b/setup.py index f4cc47d..1c34b89 100644 --- a/setup.py +++ b/setup.py @@ -40,8 +40,8 @@ 'Topic :: Communications' ], packages=['wsdiscovery', 'wsdiscovery.actions'], - setup_requires=['netifaces-plus', 'click'], - install_requires=['netifaces-plus', 'click'], + setup_requires=['ifaddr', 'click'], + install_requires=['ifaddr', 'click'], tests_require = ['pytest', 'mock'], entry_points = { 'console_scripts': [ diff --git a/wsdiscovery/threaded.py b/wsdiscovery/threaded.py index 4d8b0bf..31a3120 100644 --- a/wsdiscovery/threaded.py +++ b/wsdiscovery/threaded.py @@ -308,6 +308,7 @@ def __init__(self, observer): super().__init__(observer) def _makeMreq(self, addr): + "IPvv multicast group join/leave request" return struct.pack("4s4s", socket.inet_aton(MULTICAST_IPV4_ADDRESS), addr.packed) def _get_inet(self): @@ -334,6 +335,7 @@ def __init__(self, observer): super().__init__(observer) def _makeMreq(self, addr): + "IPv6 multicast group join/leave request" return struct.pack("=16si", socket.inet_pton(socket.AF_INET6, MULTICAST_IPV6_ADDRESS), int(addr.scope_id)) def _get_inet(self): diff --git a/wsdiscovery/util.py b/wsdiscovery/util.py index 3454e66..66958c8 100644 --- a/wsdiscovery/util.py +++ b/wsdiscovery/util.py @@ -1,16 +1,21 @@ """Various utilities used by different parts of the package.""" import io -import ipaddress import string import random -import netifaces +import logging +import ipaddress +import socket from xml.dom import minidom +import ifaddr + from .scope import Scope from .uri import URI from .namespaces import NS_ADDRESSING, NS_DISCOVERY, NS_SOAPENV from .qname import QName +logger = logging.getLogger("util") + def createSkelSoapMessage(soapAction): doc = minidom.Document() @@ -245,17 +250,29 @@ def getQNameFromValue(value, node): def _getNetworkAddrs(protocol_version): - result = [] - - for if_name in netifaces.interfaces(): - iface_info = netifaces.ifaddresses(if_name) - if protocol_version in iface_info: - for addrDict in iface_info[protocol_version]: - addr = addrDict['addr'] - ip_address = ipaddress.ip_address(addr) - if not ip_address.is_loopback: - result.append(ip_address) - return result + addrs = [] + ifaces = ifaddr.get_adapters() + + # ifaddr library only returns IPv4 and IPv6 addresses + if protocol_version == socket.AF_INET: + for iface in ifaces: + for ip in iface.ips: + if type(ip.ip) is string: + ip_address = ipaddress.ip_address(ip.ip) + if not ip_address.is_loopback: + addrs.append(ip_address) + elif protocol_version == socket.AF_INET6: + for iface in ifaces: + for ip in iface.ips: + if type(ip.ip) is tuple: + ip_address = ipaddress.ip_address(f"{ip.ip[0]}%{ip.ip[2]}") + if not ip_address.is_loopback: + addrs.append(ip_address) + else: + logger.warning(f"requested protocol version ({protocol_version}) is not" + f" IPv4 ({socket.AF_INET}) or IPv6 ({socket.AF_INET6})") + + return addrs def _generateInstanceId(): From adefa17f59e6561a8ca175ce64baa24591e3e7c4 Mon Sep 17 00:00:00 2001 From: William Poetra Yoga Date: Thu, 12 Dec 2024 15:21:35 +0700 Subject: [PATCH 2/2] Fix minor documentation error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 276a460..243079b 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ environment and set up a development environment. For example: ``` $ python3 -m venv venv $ . venv/bin/activate -(venv) $ pip3 install -e +(venv) $ pip3 install -e . ``` Then you can call `wsdiscover` and `wspublish` from within this virtual environment. Any code changes to the package will be available and testable