Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the unmaintained netifaces with psutil #283

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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