From 370c5b35ccec402b7cd85480436e2ab57c2c9ce6 Mon Sep 17 00:00:00 2001 From: Lourens Veen Date: Wed, 20 Dec 2023 18:23:30 +0100 Subject: [PATCH] Replace the unmaintained netifaces with psutil --- .../libmuscle/mcp/tcp_transport_server.py | 31 ++++++++++++------- setup.py | 2 +- tox.ini | 1 + 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/libmuscle/python/libmuscle/mcp/tcp_transport_server.py b/libmuscle/python/libmuscle/mcp/tcp_transport_server.py index 455fde1b..bd9d406c 100644 --- a/libmuscle/python/libmuscle/mcp/tcp_transport_server.py +++ b/libmuscle/python/libmuscle/mcp/tcp_transport_server.py @@ -4,7 +4,7 @@ from typing import cast, List, Optional, Tuple from typing_extensions import Type -import netifaces +import psutil from libmuscle.mcp.transport_server import RequestHandler, TransportServer from libmuscle.mcp.tcp_util import (recv_all, recv_int64, send_int64, @@ -108,15 +108,24 @@ def close(self) -> None: self._server.server_close() def _get_if_addresses(self) -> List[str]: + """Returns a list of local addresses. + + This returns a list of strings containing all IPv4 and IPv6 network + addresses bound to the available network interfaces. The server + will listen on all interfaces, but not all of them may be reachable + from the client. So we get all of them here, and the client can + then try them all and find one that works. + """ all_addresses: List[str] = [] - ifs = netifaces.interfaces() - for interface in ifs: - addrs = netifaces.ifaddresses(interface) - for props in addrs.get(netifaces.AF_INET, []): - if not props['addr'].startswith('127.'): - all_addresses.append(props['addr']) - for props in addrs.get(netifaces.AF_INET6, []): - # filter out link-local addresses with a scope id - if '%' not in props['addr'] and props['addr'] != '::1': - all_addresses.append('[' + props['addr'] + ']') + ifs = psutil.net_if_addrs() + for _, addresses in ifs.items(): + for addr in addresses: + if addr.family == socket.AF_INET: + if not addr.address.startswith('127.'): + all_addresses.append(addr.address) + if addr.family == socket.AF_INET6: + # filter out link-local addresses with a scope id + if '%' not in addr.address and addr.address != '::1': + all_addresses.append('[' + addr.address + ']') + return all_addresses diff --git a/setup.py b/setup.py index f38018c2..58f23402 100644 --- a/setup.py +++ b/setup.py @@ -55,7 +55,7 @@ 'click>=7.1,<9', 'matplotlib>=3,<4', 'msgpack>=1,<2', - 'netifaces==0.11.0', + 'psutil>=5.0.0', "numpy<1.22; python_version=='3.7'", "numpy>=1.22; python_version>='3.8'", 'qcg-pilotjob==0.13.1', diff --git a/tox.ini b/tox.ini index 9b28adea..e9d89a3e 100644 --- a/tox.ini +++ b/tox.ini @@ -8,6 +8,7 @@ deps = flake8 pytest pytest-cov + types-psutil ymmsl passenv =