-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnetwork_here.py
More file actions
42 lines (34 loc) · 1.52 KB
/
network_here.py
File metadata and controls
42 lines (34 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""Network addresses discovering."""
from collections.abc import Iterator
from kivy import platform
if platform == "android":
from jnius import autoclass # pylint: disable=import-error
NetworkInterface = autoclass("java.net.NetworkInterface")
Inet4Address = autoclass("java.net.Inet4Address")
else:
import ifaddr
def get_android_interface_addresses(
interface: "NetworkInterface",
) -> Iterator[tuple[str, str]]:
"""Yields active IPv4 addresses for given network interface."""
if interface.isUp():
addresses = interface.getInetAddresses()
while addresses.hasMoreElements():
address = addresses.nextElement()
if isinstance(address, Inet4Address) and not address.isLoopbackAddress():
yield interface.getDisplayName(), address.getHostAddress()
def get_all_available_ipv4_adrresses() -> Iterator[tuple[str, str]]:
"""Yields available interfaces with IPv4 addresses
available for connections from there.
"""
if platform == "android":
interfaces = NetworkInterface.getNetworkInterfaces()
while interfaces.hasMoreElements():
yield from get_android_interface_addresses(interfaces.nextElement())
else:
for adapter in ifaddr.get_adapters():
if adapter.name != "lo":
for ip_addr in adapter.ips:
# for IPv6 ip_addr.ip is a tuple
if isinstance(ip_addr.ip, str) and ip_addr.ip != "127.0.0.1":
yield (adapter.nice_name, ip_addr.ip)