Skip to content

Commit

Permalink
Replace the unmaintained netifaces with psutil
Browse files Browse the repository at this point in the history
  • Loading branch information
LourensVeen committed Dec 20, 2023
1 parent 3e7d118 commit 370c5b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
31 changes: 20 additions & 11 deletions libmuscle/python/libmuscle/mcp/tcp_transport_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ deps =
flake8
pytest
pytest-cov
types-psutil
ymmsl

passenv =
Expand Down

0 comments on commit 370c5b3

Please sign in to comment.