Skip to content

Commit 192c997

Browse files
committed
consolidate netifaces/psutil into _populate_from_dict
where dict is like `{"en0": ["1.2.3.4"]}`
1 parent 843fc37 commit 192c997

File tree

1 file changed

+30
-52
lines changed

1 file changed

+30
-52
lines changed

jupyter_client/localinterfaces.py

+30-52
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from typing import Any, Callable, Iterable, Sequence
1212
from warnings import warn
1313

14-
LOCAL_IPS: list = []
15-
PUBLIC_IPS: list = []
14+
LOCAL_IPS: list[str] = []
15+
PUBLIC_IPS: list[str] = []
1616

1717
LOCALHOST: str = ""
1818

@@ -75,29 +75,34 @@ class NoIPAddresses(Exception): # noqa
7575
pass
7676

7777

78-
def _populate_from_list(addrs: Sequence[str] | None) -> None:
78+
def _populate_from_list(addrs: Sequence[str]) -> None:
7979
"""populate local and public IPs from flat list of all IPs"""
80+
_populate_from_dict({"all": addrs})
81+
82+
83+
def _populate_from_dict(addrs: dict[str, Sequence[str]]) -> None:
84+
"""populate local and public IPs from dict of {'en0': 'ip'}"""
8085
if not addrs:
81-
raise NoIPAddresses
86+
raise NoIPAddresses()
8287

8388
global LOCALHOST
8489
public_ips = []
8590
local_ips = []
8691

87-
for ip in addrs:
88-
local_ips.append(ip)
89-
if not ip.startswith("127."):
90-
if not ip.startswith("169.254."):
92+
for iface, ip_list in addrs.items():
93+
for ip in ip_list:
94+
local_ips.append(ip)
95+
if not LOCALHOST and (iface.startswith("lo") or ip.startswith("127.")):
96+
LOCALHOST = ip
97+
if not iface.startswith("lo") and not ip.startswith(("127.", "169.254.")):
9198
# don't include link-local address in public_ips
9299
public_ips.append(ip)
93-
elif not LOCALHOST:
94-
LOCALHOST = ip
95100

96101
if not LOCALHOST or LOCALHOST == "127.0.0.1":
97102
LOCALHOST = "127.0.0.1"
98103
local_ips.insert(0, LOCALHOST)
99104

100-
local_ips.extend(["0.0.0.0", ""]) # noqa
105+
local_ips.extend(["0.0.0.0", ""]) # noqa: S104
101106

102107
LOCAL_IPS[:] = _uniq_stable(local_ips)
103108
PUBLIC_IPS[:] = _uniq_stable(public_ips)
@@ -157,65 +162,38 @@ def _load_ips_psutil() -> None:
157162
"""load ip addresses with netifaces"""
158163
import psutil
159164

160-
global LOCALHOST
161-
local_ips = []
162-
public_ips = []
165+
addr_dict: dict[str, list[str]] = {}
163166

164167
# dict of iface_name: address_list, eg
165168
# {"lo": [snicaddr(family=<AddressFamily.AF_INET>, address="127.0.0.1",
166169
# ...), snicaddr(family=<AddressFamily.AF_INET6>, ...)]}
167170
for iface, ifaddresses in psutil.net_if_addrs().items():
168-
for address_data in ifaddresses:
169-
if address_data.family == socket.AF_INET:
170-
addr = address_data.address
171-
if not (iface.startswith("lo") or addr.startswith("127.")):
172-
public_ips.append(addr)
173-
elif addr.startswith("169.254."):
174-
# don't include link-local address in public_ips
175-
pass
176-
elif not LOCALHOST:
177-
LOCALHOST = addr
178-
local_ips.append(addr)
179-
if not LOCALHOST:
180-
# we never found a loopback interface (can this ever happen?), assume common default
181-
LOCALHOST = "127.0.0.1"
182-
local_ips.insert(0, LOCALHOST)
183-
local_ips.extend(["0.0.0.0", ""]) # noqa
184-
LOCAL_IPS[:] = _uniq_stable(local_ips)
185-
PUBLIC_IPS[:] = _uniq_stable(public_ips)
171+
addr_dict[iface] = [
172+
address_data.address
173+
for address_data in ifaddresses
174+
if address_data.family == socket.AF_INET
175+
]
176+
177+
_populate_from_dict(addr_dict)
186178

187179

188180
def _load_ips_netifaces() -> None:
189181
"""load ip addresses with netifaces"""
190182
import netifaces # type: ignore[import-not-found]
191183

192-
global LOCALHOST
193-
local_ips = []
194-
public_ips = []
184+
addr_dict: dict[str, list[str]] = {}
195185

196186
# list of iface names, 'lo0', 'eth0', etc.
197187
for iface in netifaces.interfaces():
198188
# list of ipv4 addrinfo dicts
189+
addr_dict[iface] = []
190+
199191
ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])
200192
for entry in ipv4s:
201193
addr = entry.get("addr")
202-
if not addr:
203-
continue
204-
if not (iface.startswith("lo") or addr.startswith("127.")):
205-
public_ips.append(addr)
206-
elif addr.startswith("169.254."):
207-
# don't include link-local address in public_ips
208-
pass
209-
elif not LOCALHOST:
210-
LOCALHOST = addr
211-
local_ips.append(addr)
212-
if not LOCALHOST:
213-
# we never found a loopback interface (can this ever happen?), assume common default
214-
LOCALHOST = "127.0.0.1"
215-
local_ips.insert(0, LOCALHOST)
216-
local_ips.extend(["0.0.0.0", ""]) # noqa
217-
LOCAL_IPS[:] = _uniq_stable(local_ips)
218-
PUBLIC_IPS[:] = _uniq_stable(public_ips)
194+
if addr:
195+
addr_dict[iface].append(addr)
196+
_populate_from_dict(addr_dict)
219197

220198

221199
def _load_ips_gethostbyname() -> None:

0 commit comments

Comments
 (0)